English 中文(简体)
在通过javascript将项目从一个选定的清单转移到另一个清单时,无效的示范国家
原标题:Invalid ModelState when moving items from one select list to another via javascript

我翻了一页,其中载有两个名单,一个是空的,一个是具有结构上相同价值的人。 用户可以从有人居住的名单上挑选一个物品,点击一个吨子,然后通过 j子将物品移至空名单上。 在提交表格时,我只想把所有选择带入一个隐蔽的形式领域,然后在职位后放下。 然而,在我提交该页时,如果我从一个名单箱向另一个箱转移了价值,我会遇到以下错误(否则,页数通常):

System.InvalidOperationException: The parameter conversion from type 
 System.String  to type  System.Web.Mvc.SelectListItem  failed because no type
 converter can convert between these types. at 
 System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType) at 
 System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType) at 
 System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture) at 
 System.Web.Mvc.ValueProviderResult.ConvertTo(Type type) at 
 System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)

相关法典:

观点:

public class ViewModel
{
    public IEnumerable<SelectListItem> currentUsers { get; set; }
    public IEnumerable<SelectListItem> availableUsers { get; set; }
}

主计长 行动:

public ActionResult Index() 
{
    IEnumerable<User> availableUsers = SomeDal.GetUsers();

    var model = new ViewModel 
    {
        currentUsers = Enumerable.Empty<User>().Select(x => new SelectListItem
        {
            Value = x.id.ToString(),
            Text = x.name
        }),

        availableUsers = availableUsers.Select(x => new SelectListItem
        {
            Value = x.id.ToString(),
            Text = x.lastName
        })
    };
}

POST 主计长行动:

[HttpPost]
public ActionResult Index(ViewModel model) 
{
    // at this point, ModelState.IsValid is already true, so I figure the rest of this isn t necessary
}

移动物品的杂货:

$( #swapUsers ).click(function () {
    $( #availableUsers option:selected ).appendTo( #currentUsers );
});

The ListBoxes:

@Html.ListBox(
         "currentUsers",
         new SelectList(Model.currentUsers, "value", "text"),
         new { @class = "listbox" }
         )

@Html.ListBox(
         "availableUsers",
         new SelectList(Model.availableUsers, "value", "text"),
         new { @class = "listbox" }
         )

我首先想到的是,这是我很奇怪的。 雇员要求,但即使目前的错误发生。 用户数字包含数据。

EDIT:

传真 Post

<select class="listbox" id="currentUsers" multiple="multiple" name="currentUsers"></select>
<select class="listbox" id="availableUsers" multiple="multiple" name="availableUsers">
    <option value="1">Test User 1</option>
    <option value="2">TestUser2</option>
</select>

传真 Post

<select class="input-validation-error listbox" id="currentUsers" multiple="multiple" name="currentUsers">
    <option selected="selected" value="1">Test User 1</option>
</select>
<select class="listbox" id="availableUsers" multiple="multiple" name="availableUsers">
    <option value="2">TestUser2</option>
</select>
最佳回答

考虑到提交表格时张贴到服务器的模型将只包括选定的数值清单;你不展示/分析,但正如你在提交文件上采取的行动一样,接受与你打算显示表格的相同的看法。 ViewModel类别对每种特性都有可数的E,而你正在发现的错误是,模型Binder可以将一个阵列清单转换成一个可计算和设计的IE;User> (或 IEvidable<SelectListItem>......) 略微混淆了这一差异; 或许你们的模型(或参数)出现在方法上,它有一个可计算和参照的电子计算法;SelectListItem>?

实际上,提交表格时应当有一个模式,接受两个清单(或实际上可能只需要目前的用户名单),作为分类(或实际类型)。

public ActionResult PostMethod(IEnumerable<int> currentUsers)
{
    ...
}
问题回答

暂无回答




相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签