簡體   English   中英

DropDownListFor報告類型不匹配

[英]DropDownListFor reports type mismatch

模型:

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

嘗試更改:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM