>故障排除“ system.int32” vs.“ iEnumerable”下拉列表中的错误 >提交带有下拉列表的表单有时会导致错误指示类型不匹配的错误:
>项目期望> type(用于下拉列表选项),但会接收ViewData
>。
IEnumerable
System.Int32
根本原因:null类别列表
> >这通常是因为视图模型中的属性(例如,
)是>时。 CategoryList
依靠此属性填充下拉列表。ProjectVM
>
null
ViewData
解决方案:在“邮政方法”中重新填充类别列表
> 解决方案是在返回视图之前明确地重新填充控制器的post Action方法之前的
属性。 这样可以确保在>中可用正确的数据。
CategoryList
这是修改您的帖子方法的方法:
IEnumerable
添加此行重新创建ViewData
并将其分配给
>数据。
<code class="language-csharp">public ActionResult Create(ProjectVM model) { if (!ModelState.IsValid) { // Repopulate the CategoryList model.CategoryList = new SelectList(db.Categories, "ID", "Name"); return View(model); } // ... (Your existing code to save the model and redirect) ... }</code>
SelectList
>
model.CategoryList
IEnumerable
helper方法(和DropDownListFor()
)试图找到适当的
,则使用属性名称(例如'categoryId')在中查找数据。如果项目不可铸入
,则会发生不匹配错误。 通过在邮政方法中明确设置,我们绕过了此问题。
以上是为什么我的下拉列表抛出' system.int32”与' iEnumerable”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!