【发布时间】:2021-05-28 08:09:35
【问题描述】:
当我在我的添加产品页面上填写信息并尝试保存时,我得到了错误。
public ActionResult Create()
{
List<SelectListItem> values= (from i in db.Categories.ToList()
select new SelectListItem
{
Text = i.Name,
Value = i.Id.ToString()
}
).ToList();
ViewBag.val = values;
return View();
}
查看
<div class="form-group">
@Html.LabelFor(model => model.CategoryId, "CategoryId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(m=> m.CategoryId ,(List<SelectListItem>)ViewBag.val, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryId, "", new { @class = "text-danger" })
</div>
</div>
错误
System.InvalidOperationException: '具有键 'CategoryId' 的 ViewData 项的类型为 'System.Int32' 但必须为 'IEnumerable' 类型。'
【问题讨论】:
标签: c# model-view-controller razor