【发布时间】:2011-07-20 05:16:47
【问题描述】:
就像this poster,我对 ASP.NET MVC Html.ListBoxFor(...) 有点困惑。具体来说,我将选择结果放在列表中,但在发布结果后,我得到了
InvalidOperationException: The ViewData item that has the key 'SelectedDeclarations' is of type 'System.String[]' but must be of type 'IEnumerable<SelectListItem>'
这是我传递给强类型剃刀视图的缩写 ViewModel
public MyViewModel
{
public MyViewModel()
{
(...)
this.VendorsRequiringDeclaration = new List<SelectListItem>();
this.SelectedDeclarations = new List<String>();
}
public IEnumerable<String> SelectedDeclarations { get; set; }
public List<SelectListItem> VendorsRequiringDeclaration { get; set; }
}
这里是引用它们的视图代码
@Html.ListBoxFor(m=>m.SelectedDeclarations, Model.VendorsRequiringDeclaration, new { @class="editor-field", @size=6})
如果我将 MyViewModel 更改为 SelectedDeclarations 是 SelectedListItem 列表而不是 String 列表,则在发布到适当的控制器操作后,它会认为我的模型无效:
{"The parameter conversion from type 'System.String' to type 'System.Web.Mvc.SelectListItem' failed because no type converter can convert between these types."}
想法?我可能对第一个参数有错误的 LINQ 表达式,但我无法从类似的问题中看到它。提前致谢!
【问题讨论】:
-
看看这个问题,它可能会有所帮助:stackoverflow.com/questions/2308846/…
标签: asp.net-mvc