1

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.

singha4086
  • 73
  • 2
  • 7
  • 1
    I assume this is happening when you submit the form and return the view. Its because `ViewBag.Roles` is `null` (you cannot create a `SelectList` from `null`). But `ViewBag.Roles` is already `IEnumerable` so creating another identical one using `new SelectList(...)` is just pointless extra overhead. Just use `@Html.DropDownListFor(m => m.RoleName, (IEnumerable)ViewBag.Roles, new { ... })` –  Apr 26 '17 at 02:54
  • Possible duplicate of [Value cannot be null. Parameter name: items (in Dropdown List) ASP.NET MVC5](http://stackoverflow.com/questions/38921483/value-cannot-be-null-parameter-name-items-in-dropdown-list-asp-net-mvc5) or http://stackoverflow.com/questions/18357321/value-cannot-be-null-parameter-name-items-drodownlist. – Tetsuya Yamamoto Apr 26 '17 at 02:55
  • @StephenMuecke I used your suggestion, and its throwing an error saying: The ViewData item that has the key ` 'RoleName' is of type 'System.String' but must be of type 'IEnumerable'' `. I am guessing this is because I do not have a Role list defined in my model. Please correct me if I am wrong. Thank you. – singha4086 Apr 26 '17 at 03:13
  • Read the first comment - its because `ViewBag.Roles` is `null`! - you need to repopulate it in the POST method before you return the view. And refer [this answer](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  Apr 26 '17 at 03:30
  • 1
    @Stephen Muecke I hope you bath in the pond of luxury for rest of your life. It works. Thank you so much. – singha4086 Apr 26 '17 at 03:51

0 Answers0