2016-02-13 28 views
0

我想學習使用ASP.NET身份向註冊頁面添加下拉菜單。我可以下拉顯示出來時,它是在一個視圖中的唯一元素,但是當我嘗試將其添加到默認的「註冊」看來,我得到一個錯誤:Asp.net身份將下拉列表添加到默認註冊視圖

The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Identity.Web.Models.Countries]', but this dictionary requires a model item of type 'Identity.Web.Models.RegisterViewModel'.

這裏是什麼作品本身:

型號:

public class Countries 
{ 
    public string Country { get; set; } 
    public string CountryAbbr { get; set; } 
    //for drop down menu 
    public SelectList CountryList { get; set; } 
} 

查看

@model DDL_Test3.Models.Countries 
@{ViewBag.Title = "DropDown"} 

@Html.DropDownListFor(model => model.Country, 
new SelectList(ViewBag.CountryList, "CountryAbbr", "Country"), "--Select a Country --", new { @class = "form-control" }) 

控制器:

public ActionResult DropDown() 
    { 
     List<Countries> model = SQLConnection.ddlGetAllCountries(); 
     ViewBag.CountryList = model; 
     return View(); 
    } 

    public static List<Countries> ddlGetAllCountries() 
    { 
     using (var connection = new SqlConnection(SQLConnection.GetConnectionString())) 
     { 
      return connection.Query<Countries>("sp_GetAllCountries", commandType: CommandType.StoredProcedure).ToList(); 
     } 
    } 

我想我感到困惑,就是如何獲取下拉列表添加到默認的 'RegisterViewModel',下面顯示:

public class RegisterViewModel 
{ 
    [Required] 
    [Display(Name = "UserName")] 
    public string UserName { get; set; } 

    [Required] 
    [EmailAddress] 
    [Display(Name = "Email")] 
    public string Email { get; set; } 

    [Required] 
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

    [Display(Name = "Select a Country")] 
    public string Country { get; set; } 
    public string CountryAbbr { get; set; } 
    //drop down list 
    public System.Web.Mvc.SelectList CountryList { get; set; } 

} 

已更新

以下是缺少的控制器:

// GET: /Account/Register 
    [AllowAnonymous] 
    public ActionResult Register() 
    { 
     List<Countries> model = EntityData.ddlGetAllCountries(); 
     ViewBag.CountryList = model; 

     return View(model); 
    } 

這裏是@RegisterViewModel觀點:

@model Identity.Web.Models.RegisterViewModel 
@{ 
    ViewBag.Title = "Register"; 
} 

<h2>@ViewBag.Title.</h2> 

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
{ 
    @Html.AntiForgeryToken() 
    <h4>Create a new account.</h4> 
    <hr /> 
    @Html.ValidationSummary("", new { @class = "text-danger" }) 
    <div class="form-group"> 
     @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(m => m.Country, new { @class = "col-md-2 control-label" }) 
     <div class="col-md-10"> 
      @Html.DropDownListFor(model => model.Country, new SelectList(ViewBag.CountryList, "CountryAbbr", "Country"), "--Select a Country --", new { @class = "form-control" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" class="btn btn-default" value="Register" /> 
     </div> 
    </div> 
} 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 
+0

該錯誤的性質是自我解釋 - 你的錯型號傳遞給視圖。但你甚至沒有顯示相關的代碼。沒有你在哪裏顯示了具有'@model RegisterViewModel'的視圖,並且沒有顯示控制器方法返回列表' –

+0

'謝謝你的回覆。我在原始文章中添加了一些缺失的元素。我瞭解錯誤,我不知道如何正確添加下拉菜單到已有的'RegiserViewModel' – PixelPaul

回答

1

你的GET方法傳遞的List<Countries> model = EntityData.ddlGetAllCountries();的模型,其預計,該模型是@model Identity.Web.Models.RegisterViewModel

更改get方法

視圖
public ActionResult Register() 
{ 
    var countries = EntityData.ddlGetAllCountries(); 
    RegisterViewModel model = new RegisterViewModel 
    { 
     CountryList = new SelectList(countries, "CountryAbbr", "Country") 
    }; 
    return View(model); 
} 

並改變視圖以使用

@Html.DropDownListFor(m => m.Country, Model.CountryList, "--Select a Country --", new { @class = "form-control" }) 

沒有必要使用ViewBag因爲你已經模型包含了SelectList

+0

謝謝@Stephen Muecke,這正是需要的! – PixelPaul