Marian Jordanov Marian Jordanov - 1 year ago 85
C# Question

C# razorview DropDownListFor 'Value cannot be null'

I am new to ASP.NET MVC and I'm working on my first project just for fun.
I get this ArgumentNullException and I cannot figure out what's wrong.

This is my model:

public class SpeciesLabel
{
[Key]
[Required]
public string Name { get; set; }

[Required]
public CustomGroup CustomGroup { get; set; }

[Required]
public Family Family { get; set; }

[Required]
public Genus Genus { get; set; }

[Required]
public Species Species { get; set; }
}

public class SpeciesLabelDbContext : DbContext
{
public SpeciesLabelDbContext()
: base("DefaultConnection")
{

}

public DbSet<SpeciesLabel> SpeciesLabel { get; set; }
}


This is the controller:

public ActionResult Create()
{
List<SelectListItem> customGroups = new List<SelectListItem>();
IQueryable<string> customGroupsQuery = from g in customGroupsDb.CustomGroup
select g.Name;

foreach (var element in customGroupsQuery)
{
customGroups.Add(new SelectListItem()
{
Value = element,
Text = element
});
}

ViewBag.CustomGroup = customGroups;


This is the controller POST request:

public ActionResult Create([Bind(Include = "CustomGroup,Family,Genus,Species")] SpeciesLabel speciesLabel)
{
if (ModelState.IsValid)
{
db.SpeciesLabel.Add(speciesLabel);
db.SaveChanges();
return RedirectToAction("Create");
}

return View();
}


And this is the view:

<pre>
@model PlantM.Models.PlantModels.SpeciesLabel

@{
ViewBag.Title = "Create";
}

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>Species label</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })

<div class="form-group">
@Html.LabelFor(model => model.CustomGroup, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CustomGroup, new SelectList(ViewBag.CustomGroupList, "Value", "Text"), "Please select...", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CustomGroup, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
</pre>


I have inputs for all properties in the view but I cut them as they are similar to this one and the exception would be the same. Only property Name is not returned from the view as it will be designed in the controller (concatenation of the other properties).

This is the exception I get when I submit the form:

ArgumentNullException

Edit:

After adding the ViewBag initialization in the POST Create method the problem with the ArgumentNullException is resolved but I still receive Null value arguments and the object cannot be created due to this and the Create view is recalled again and again!? Can anyone advise why these @Html.DropDownListFor do not post any value to the controller?

Answer Source

From the comment, it sound like you see the view on first visit, but a null exception happen after you post.

If above assumption is correct, then I think your problem is because when you post back, your model did not pass the validation (for example, maybe a required input field did not post back value), which means ModelState.IsValid is false, so return View() was called

Here is the problem, you are not setting the ViewBag.CustomGroup = customGroups; in before return, hence ViewBag.CustomGroup is null, that is why you are seeing the exception.

init the ViewBag like how you did it on get then you should be able to see the page.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download