我翻了一页,其中载有两个名单,一个是空的,一个是具有结构上相同价值的人。 用户可以从有人居住的名单上挑选一个物品,点击一个吨子,然后通过 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>