Value cannot be null. Parameter name: items (DrodownList)
Asked Answered
W

1

5

I'm working with ASP.NET MVC4 and now I want to add a dropdownlist with data from my mysql database. This is what I do :

In my view (Register.cshtml):`

<div class="control-group">
    @Html.LabelFor(m => m.DistrictID, new { @class= "control-label"})
    <div class="controls">
        @Html.DropDownListFor(model => model.DistrictID, new SelectList(ViewBag.Districts, "district_id", "district_name", Model.DistrictID))
    </div>
</div>

In my Controller (AccountController):

[AllowAnonymous]
public ActionResult Register()
{
    var districts = repository.GetDistricts();
    ViewBag.Districts = districts;

    return View(new RegisterModel());
}

//
// POST: /Account/Register

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{

    if (ModelState.IsValid)
    {
        // Attempt to register the User
        try
        {
            MembershipService.CreateUser(model.Name, model.FamilyName, model.BirthDate, model.Sex, model.Nationality, model.Email, model.UserName, model.Password, model.Street, model.StreetNr);

            FormsAuthentication.SetAuthCookie(model.UserName, false);
            return RedirectToAction("Index", "Home");
        }
        catch (ArgumentException ae)
        {
            ModelState.AddModelError("", ae.Message);
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

I get the Districts from my Repository like this:

 public IQueryable<district> GetDistricts()
 {
     return from district in entities.districts
            orderby district.district_name
            select district;
 }

My RegisterModel:

public class RegisterModel
{
    [Required]
    [Display(Name = "Given name")]
    public string Name { get; set; }

    [Required]
    [Display(Name = "Family name")]
    public string FamilyName { get; set; }

    [Required]
    [Display(Name = "Birthdate")]
    public DateTime BirthDate { get; set; }

    [Required]
    [Display(Name = "Sex")]
    public string Sex { get; set; }

    [Required]
    [Display(Name = "Nationality")]
    public string Nationality { get; set; }

    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [Display(Name = "User name")]
    public string UserName { 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; }

    [Required]
    [Display(Name = "Street")]
    public string Street { get; set; }

    [Required]
    [Display(Name = "Street Number")]
    public int StreetNr { get; set; }

    [Required]
    [Display(Name = "District")]
    public IEnumerable<SelectListItem> Districts { get; set; }

    public int DistrictID { get; set; }
}

The dropdownlist is filled with districts but when I click on "Register" I get this error:

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

When I debug the method I see that the ModelState is NOT valid.
Key 11 is DistrictID and includes the districtid but Key 12 is Districts and gives the error: "The district field is required" ...

What am I doing wrong?

Weeks answered 21/8, 2013 at 12:23 Comment(2)
Exception is thrown only when validation fails, or when validation is passed as well?Fehr
Possible duplicate of What is a NullReferenceException, and how do I fix it?Corked
F
7

Consider the case when model validation fails. View will be redisplayed again with model sent with the request. However this line:

new SelectList(ViewBag.Districts, "district_id", "district_name", Model.Districts)

will have null as a first parameter, since ViewBag.Districts was not repopulated, causing the exception. So in order to avoid exception just set this property again:

// If we got this far, something failed, redisplay form
var districts = repository.GetDistricts();
ViewBag.Districts = districts;
return View(model);

Update. When seeing the model definition, thing that immediately comes into the mind is Required attribute of the Districts collection. Most likely you do not need user to enter any of those, nor you save them into the database. Try removing this attribute, and error about this property will disappear.

Fehr answered 21/8, 2013 at 12:28 Comment(8)
Now, when I click on "Register" it just reloads the page and the values are still filled in but nothing happens... Also didn't added it into the databaseWeeks
@niels123, have you tried debugging? At least what you can do is to use some meaningful key instead of empty string for AddModelError and display it somewhere on the page with Html.ValidationMessage - this will show if the exception occurs. But still, the best way to understand what goes wrong is just to debug the method.Fehr
I checked the method and the modelstate isn't valid. Key 11 (DistrictID) has the value of my districtid but Key 12 (Districts) gives an error: "The district field is required".Weeks
@niels123, error message is pretty self-explaining. Have you chosen anything in the district dropdown before hitting "register"?Fehr
@niels123, I believe I finally got it. You should specify that your drop down is for DistrictID, in other words it defines value for this property. Try this code: @Html.DropDownListFor(model => model.DistrictID,...Fehr
Changed it, but still the same error ... "The District field is required"Weeks
@niels123, impossible to answer without seeing the model. Can you share its definition?Fehr
Added it to my beginpost!Weeks

© 2022 - 2024 — McMap. All rights reserved.