这是我的下拉列表:
<div class="editor-label">
<p>
<%: Html.LabelFor(model => model.Gov) %>
</p>
</div>
<div class="editor-field">
<p>
<%=Html.DropDownListFor(model => model.Gov, ViewBag.gov as SelectList)%>
<%: Html.ValidationMessageFor(model => model.Gov) %>
</p>
这是我的视图控制器:
public ActionResult TestR()
{
ViewBag.gov = new SelectList(entity.gouvernerat, "Idgov", "Nomg");
return View();
}
使用 HttpPost :
[HttpPost]
public ActionResult TestR(LogModel.preRegister Model)
{
if (ModelState.IsValid)
{
if (LogModel.preRegister.Verifuser(Model.IDag))
{
ModelState.AddModelError("", "Agence existe deja");
return View(Model);
}
else
{
int ins = LogModel.preRegister.Register(Model);
if (ins > 0)
{
return View("Index");
}
else return View();
}
}
else
{
return View();
}
}
现在下拉列表显示我的数据库中的政府列表(这就是我想要的),但是当我点击创建时,我的 DropdownLisrt 中出现了这个错误There is no ViewData item of type 'IEnumerable <SelectListItem>' with key 'Gov'.