我对此感到非常困惑,我觉得我一定是错过了一些非常明显的东西。
我有一个视图模型,它包含以下属性:
public class ExampleModel
{
public int CourseId { get; set; }
public List<SelectListItem> CourseList { get; set; }
}在我看来,我使用Html.DropDownListFor()助手方法的方式如下:
@Html.DropDownListFor(m => m.CourseId, Model.CourseList)这可以在初始页面加载时使用。但是,当我单击submit,在表单中某个地方出现错误时,我的控制器尝试再次加载form视图以显示错误,然后接收到这个异常:
The ViewData item that has the key 'CourseId' is of type
'System.Int32' but must be of type 'IEnumerable<SelectListItem>'.下面一行突出显示:
@Html.DropDownListFor(m => m.CourseId, Model.CourseList)我的问题很简单:为什么?为什么CourseId需要成为IEnumerable of SelectListItem才能作为该方法中的第一个参数?我检查了该方法的所有可用签名,其中没有一个使用IEnumerable of SelectListItem作为第一个参数。那是怎么回事?
发布于 2014-05-14 02:26:03
我想出来了..。呃。我不知道为什么会出现这样的错误,但问题是我的CourseList没有第二次被填充,因此是空的。修正了错误。
至于为什么错误信息如此离谱,我仍然不确定。
https://stackoverflow.com/questions/23644568
复制相似问题