我正在尝试学习使用ASP.NET标识向注册页面添加下拉菜单。当它是视图中唯一的元素时,我可以让下拉显示,但是当我尝试添加它时,默认情况下注册'看来,我收到了错误:
传递到字典中的模型项的类型是' System.Collections.Generic.List`1 [Identity.Web.Models.Countries]',但是这个字典需要一个类型为&的模型项#39; 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();
}
}
我想我感到困惑的是,如何将下拉列表添加到默认的&#39; RegisterViewModel&#39;,如下所示:
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 :(得分: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