我正在DropDownList
从内存数据中填充并在 POST 上收到此错误。
错误:具有键“MaritalStatus”的 ViewData 项属于“System.String”类型,但必须属于“IEnumerable”类型。
控制器:-
// GET: /Application/Create
public ActionResult Create()
{
List<SelectListItem> lst = new List<SelectListItem>();
lst.Add(new SelectListItem { Text = "Unmarried", Value = "1" });
lst.Add(new SelectListItem { Text = "Married", Value = "2" });
lst.Add(new SelectListItem { Text = "Widow", Value = "3" });
ViewBag.MaritalStatus = new SelectList(lst, "Value", "Text");
return View();
}
// POST: /Application/Create
[HttpPost]
public ActionResult Create(ApplicationForm applicationform)
{
if (ModelState.IsValid)
{
db.ApplicationForms.Add(applicationform);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(applicationform);
}
看法:-
<div class="editor-label">
@Html.LabelFor(model => model.MaritalStatus)
</div>
<div class="editor-field">
@Html.DropDownList("MaritalStatus")
@Html.ValidationMessageFor(model => model.MaritalStatus)
</div>
模型属性:-
[DisplayName("Marital Status")]
public string MaritalStatus { get; set; }
任何帮助表示赞赏。