我正在尝试使用 MVC4 提交表单,但似乎无法弄清楚为什么提交后会出现错误。我已经粘贴了下面的所有相关代码以及我看到的错误。
错误:
The ViewData item that has the key 'Group' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'.
控制器:
[HttpGet]
public ActionResult CreateRole()
{
var RM = new myModel();
RM.Groups = RM.InitializeGroupList();
return View(RM);
}
[HttpPost]
public ActionResult Myfunction(myModel model)
if (ModelState.IsValid)
{
//Execute Update
}
模型:
[Required]
[Display(Name = "Group")]
public string Group { get; set; }
public IEnumerable<SelectListItem> Groups;
public IEnumerable<SelectListItem> InitializeGroupList()
{
List<SelectListItem> topOfList = new List<SelectListItem>
{
new SelectListItem { Value = string.Empty, Text = "Category" },
new SelectListItem { Value = "Administrator", Text = "Administrator" },
new SelectListItem { Value = "Partner", Text = "Partner" },
new SelectListItem { Value = "Internal", Text = "Internal" }
};
//IEnumerable<SelectListItem> list = db.GetRoleList(topOfList);
IEnumerable<SelectListItem> list = topOfList;
return new SelectList(list, "Value", "Text");
}
[Required]
[Display(Name = "Group")]
public string Group { get; set; }
public IEnumerable<SelectListItem> Groups;
看法:
@Html.DropDownListFor(model => model.Group, Model.Groups)