Asp.net 标识将下拉列表添加到默认注册视图



我正在尝试学习使用 ASP.NET 身份向注册页面添加一个下拉菜单。当它是视图中的唯一元素时,我可以让下拉列表显示出来,但是当我尝试将其添加到默认的"注册"视图时,我收到一个错误:

传递到字典中的模型项的类型为"System.Collections.Generic.List'1[Identity.Web.Models.Country]",但此字典需要类型为"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")
}

您的 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的属性

相关内容

  • 没有找到相关文章

最新更新