When I have the following form:
<input type="checkbox" name="modules" value="1" />
<input type="checkbox" name="modules" value="2" />
<input type="checkbox" name="modules" value="3" />
This wil correctly send to the following MVC controller:
[HttpPost]
public ActionResult Submit(string[] modules)
{
}
But what when the above form is wrapped into an for-loop, like this:
@for (int i = 0; i < cart.Events.Count; i++)
{
<h1>@cart.Events[i].Name</h1>
<input type="checkbox" name="modules" value="1" />
<input type="checkbox" name="modules" value="2" />
<input type="checkbox" name="modules" value="3" />
}
With a controller that looks something like this:
[HttpPost]
public ActionResult Submit(TestObject modules)
{
}
public class TestObject
{
public string[] modules { get; set; }
}
That will not work correctly, because MVC doesn't know how to bind the form data to the object. I want to send the selected values only, because it's not said that the count of module checkboxes is always the same.
How to fix this?