Currently I am attempting to create a mass update based on a series of checkboxes. I can’t seem to find the correct way to do it using the entity framework.
I am using ASP.Net 4 with MVC3 and Razor.
Here is the code I have so far.
View Page (Working Properly)
@model List<LeagueCounters.Models.champion>
<form name="setFree id="setFree" method="POST" action="/Champion/SetFree">
@foreach (var item in Model)
{
if (item.isFree == true)
{
<input type="checkbox" id="@item.id" checked="checked" /> @Html.DisplayFor(modelItem => item.displayName)
}
else
{
<input type="checkbox" id="@item.id" /> @Html.DisplayFor(modelItem => item.displayName)
}
}
<p><input type="submit" value="Save" /></p>
Controller (Errors)
[HttpPost, Authorize(Roles = "Admin")]
public ActionResult SetFree(FormCollection fcMain)
{
var sortedList = from c in _db.champions
orderby c.name
select c;
int counter = 0;
foreach (champion champ in sortedList)
{
if (fcMain[counter].Contains("true"))
champ.isFree = true;
else
champ.isFree = false;
_db.champions.Attach(champ);
_db.ObjectStateManager.ChangeObjectState(champ, EntityState.Modified);
counter++;
}
_db.SaveChanges();
return View();
}
Errors
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Question
How do I properly cycle through the checkboxes in the controller? And will my mass update code work once I get to that point?
Thanks in Advance.
The problem here is that only the checked checkboxes are posted to the server. So you can’t expect all of your checkbox keys to be present in the form collection. To workaround this behavior
Html.CheckBoxForcreates an additional hidden field with checkbox so the result could be read on the server side. I am not sure how would you use strongly typed helper here because you are rendering data in a loop. I think you can have a look at this google search