I am trying to add users to role and am getting "values cannot be null" in the dropdownlist section of the register.cshtml class.
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();
}
Model
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; }
I can create roles but I can add or register a new user with certain roles. May be I am missing something. I was just trying to practice some MVC examples and I came across this and I have been stuck for a while. Please, let me know if you guys need more information. I would appreciate any guidance. Thank you.