模型:
[Required(ErrorMessage = "You must assign this user a role")]
[Display(Name = "Role")]
public string UserRole { get; set; }
public IEnumerable<System.Web.Mvc.SelectListItem> UserRoles { get; set; }
控制器:
public ActionResult CreateNewUser()
{
CreateNewUserModel model = new CreateNewUserModel();
model.UserRoles = new []
{
new SelectListItem{Value="Administrator", Text="Administrator"},
new SelectListItem{Value="User", Text="User"}
};
//model.UserRole = "Administrator";
return View(model);
}
看法:
@Html.DropDownListFor(x => x.UserRole, Model.UserRoles,"Select a role")
@Html.ValidationMessageFor(x=>x.UserRole)
加载创建新用户的页面时,我有一个异常说明UserRole must be of type IEnumerable<SelectList>