I am using ASP.Net MVC4 (Razor). I have the following code:
Dictionary<string, OccasionObject> occasionList = new Dictionary<string, OccasionObject>()
The key is a string of the category of the occasion. The occassion object has 3 properties: isAttending(bool), ID(int), and Name(string)
In my cshtml file, I do the following:
@foreach(string s in model.occasionList .Keys)
{
foreach(var o in model.occasionList .Keys[s])
{
@Html.CheckBoxFor(m=>m.occasionList[s].FirstOrDefault(ev=>ev.ID == o.ID).isAttending);
}
}
This binds perfectly on the load, checking boxes that I have manually checked in SQL. However, when I POST this model back to the server, the occasionList dictionary is null. The model is binding fine because other properties I have in the model are still returned.
Any ideas?
Thanks,
Dom
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The model binder treats the dictionary as a collection, if you imagine the dictionary as an IEnumerable<KeyValuePair<string, IEnumerable<OccasionObject>>> it is easy to understand why it isn’t bound.
What @Html.CheckBoxFor(m=>m.occasionList[s].FirstOrDefault(ev=>ev.ID == o.ID).isAttending); is generating is:
<input type="checkbox" name="occasionList[0].Value.isAttending" ../>
so the Key is missing.
Try this:
@Html.Hidden("occasionList.Index", s)
@Html.CheckBoxFor(m=>m.occasionList[s].FirstOrDefault(ev=>ev.ID == o.ID).isAttending);
@Html.HiddenFor(m=>m.occasionList[s].Key)
The first hidden is because you potentially will have your indexes out of order, and explicitly providing an “.Index” is the only way to have the model binder work under those circumstances.
Here’s another resource that describes model binding to collections.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0