我正在尝试将用户添加到角色中并且正在获取"值不能为空"在register.cshtml类的dropdownlist部分。
Register.Cshtml
@model BlogMVC.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.RoleName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m=>m.RoleName, new SelectList(ViewBag.Roles,"Value","Text"),new {@class="form-control"})
</div>
</div>
的AccountController
public ActionResult Register()
{
List<SelectListItem> list = new List<SelectListItem>();
foreach (var role in RoleManager.Roles)
{
list.Add(new SelectListItem() {Value = role.Name, Text = role.Name});
}
ViewBag.Roles = list;
return View();
}
模型
public class RegisterViewModel
{
[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; }
public string RoleName { get; set; }
我可以创建角色,但我可以添加或注册具有特定角色的新用户。可能是我错过了什么。我只是想练习一些MVC示例,我遇到了这个问题,而且我已经被困了一段时间。如果你们需要更多信息,请告诉我。我很感激任何指导。谢谢。