Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

c# - Value cannot be null. Parameter name: items (in Dropdown List) ASP.NET MVC5

I have problem in my code. I'm using the registration form which comes with MVC5 , I added a field "Role" as a Dropdownlist to assign a role to the user while creating a new user. like in the below image: enter image description here

Now in order to do that, I modified the "RegisterViewModel" and added the following properties:

        public IdentityRole Role { get; set; }

        [Required]
        [Display(Name = "Roles List")]
        public IEnumerable<IdentityRole> RolesList { get; set; }

In "AccountController", I changed the Register action, that gets the registration form, to become like this:

// GET: /Account/Register
        [AllowAnonymous]
        public ActionResult Register()
        {

            var _context = new ApplicationDbContext();
            var roles = _context.Roles.ToList();

            var viewModel = new RegisterViewModel
            {
                RolesList = roles
            };
            return View(viewModel);

        }

In the view "Register.cshtml" I added this Dropdownlist to load roles in the view and to post the role to the controller:

<div class="form-group">
        @Html.LabelFor(m => m.Role.Id, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.DropDownListFor(m => m.Role, new SelectList(Model.RolesList, "Name", "Name"), "Select Role", new { @class = "form-control" })
        </div>
    </div>

in the Register controller, in the the registration form post, I added this

// POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.UserName , centerName = model.centerName };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    var role = new IdentityRole(model.Role.Name);

                    //I added this line to store the user and its roles in AspNetUserRoles table:
                    await UserManager.AddToRoleAsync(user.Id, role.Name);

                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

Now when I try to register the user and post the form, I get following error:

Server Error in '/' Application.

Value cannot be null.
Parameter name: items

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: items

Source Error: 


Line 41:         @Html.LabelFor(m => m.Role.Name, new { @class = "col-md-2 control-label" })
Line 42:         <div class="col-md-10">
Line 43:             @Html.DropDownListFor(m => m.Role, new SelectList(Model.RolesList, "Name", "Name"), "Select Role", new { @class = "form-control" })
Line 44:         </div>
Line 45:     </div>

Source File: c:..TransactionsSystemViewsAccountRegister.cshtml    Line: 43 

I tried different solutions to solve it, but nothing worked, can anybody please help or advise?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot bind a <select> element to a complex object (which is what Role is) and when you submit the form, ModelState is invalid (your trying to bind the selected Name property of RolesList to typeof IdentityRole). You then return the view, but have not repopulated the RolesList property, so its null (hence the error).

View models should not contain data models, and you view model should be

public class RegisterViewModel
{
    ....
    [Required(ErrorMessage = "Please select a role")]
    public string Role { get; set; }
    public IEnumerable<SelectListItem> RoleList { get; set; }
}

and in the GET method

var roles = _context.Roles.Select(r => r.Name);
var viewModel = new RegisterViewModel
{
    RolesList = new SelectList(roles)
};
return View(viewModel);

and in the view

@Html.LabelFor(m => m.Role, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
    @Html.DropDownListFor(m => m.Role, Model.RolesList, "Select Role", new { @class = "form-control" })
</div>

this will solve the invalid ModelState issue relating to Role, but if you do need to return the view because of other ModelState issues, then you must first repopulate the collection before returning the view

if (!ModelState.IsValid)
{
    var roles = _context.Roles.Select(r => r.Name);
    model.RolesList = new SelectList(roles);
    return View(model);
}
.... // save and redirect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...