Invalid ModelState when moving items from one select list to another via javascript
I've got a page that contains two lists, one empty, one populated with values, both identical in structure. A user can select an item from the populated list, click a button, and it will move to the empty list via jQuery. When the form is submitted, I save all the option ids into a hidden form field to be later parsed out after post. However, when I submit the page, if I have moved a value from one listbox to the other, I encounter the following error (otherwise, the page posts normally):
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)
Here is the relevant code:
ViewModel:
public class ViewModel
{
public IEnumerable<SelectListItem> currentUsers { get; set; }
public IEnumerable<SelectListItem> availableUsers { get; set; }
}
Initial Controller Action:
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 Contro开发者_开发知识库ller Action:
[HttpPost]
public ActionResult Index(ViewModel model)
{
// at this point, ModelState.IsValid is already true, so I figure the rest of this isn't necessary
}
The jQuery that moves items:
$('#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" }
)
I had thought at first it was because of my strange Enumerable.Empty call, but the error occurs even if the currentUsers IEnumerable contains data.
EDIT 1:
HTML Pre-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>
HTML Post-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>
Keep in mind that the model that is getting posted to the server when the form is submitted will only include the list of selected values; You don't show/say, but it sounds like your action on submit is accepting the same ViewModel as the one you are using to display the form. The ViewModel class has IEnumerable for each of the properties, and the error you are getting is because the ModelBinder can't convert a list of strings to an IEnumerable<User> (or IEnumerable<SelectListItem>...A bit confused about that difference; Maybe your Model coming in (or parameters) to the method has an IEnumerable<SelectListItem>?).
Really, the form submission should have a model coming in that accepts the two lists (or actually it probably only needs the currentUsers list) as a list of integers (or whatever the actual type is).
public ActionResult PostMethod(IEnumerable<int> currentUsers)
{
...
}
精彩评论