0

模型:

    [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>

4

1 回答 1

0

尝试改变:

model.UserRoles = new []
    {
        new SelectListItem{Value="Administrator", Text="Administrator"},
        new SelectListItem{Value="User", Text="User"}
    };

至:

model.UserRoles = List<SelectListItem>
    {
        new SelectListItem{Value="Administrator", Text="Administrator"},
        new SelectListItem{Value="User", Text="User"}
    };
于 2013-02-14T22:00:14.030 回答