2013-02-14 58 views
0

型號:DropDownListFor報告類型不匹配

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

回答

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"} 
    };