Shahin Dohan
Shahin Dohan

Reputation: 6922

MVC3 Pass bool array of checkbox values to controller

UPDATE: Turns out I had skipped one checkbox (4) as can be seen from the code. Silly mistake from my side!

I have 16 checkboxes in my view, and I want each one of them bound to an element in a bool array (or BitArray) in the model.

If I try to do this, after submitting the form the controller only gets the first 4 elements of the array (correct values though), so then I tried to allocate more space in the constructor of the model but then the controller would never even get called. Is there any way I can get this to work, or do I need a bool variable for each checkbox?

The array:

public bool[] ActiveSettings { get; set; }

View:

<p><b>Active Settings:</b></p>
<div style="float:left">
@Html.CheckBoxFor(model => model.NetworkSettings.ActiveSettings[0]) val0<br />
@Html.CheckBoxFor(model => model.NetworkSettings.ActiveSettings[1]) val1<br />
@Html.CheckBoxFor(model => model.NetworkSettings.ActiveSettings[2]) val2<br />
@Html.CheckBoxFor(model => model.NetworkSettings.ActiveSettings[3]) val3<br />
@Html.CheckBoxFor(model => model.NetworkSettings.ActiveSettings[5]) val4<br />
@Html.CheckBoxFor(model => model.NetworkSettings.ActiveSettings[6]) val5<br />
@*etc..*@
</div>

Here's a screenshot of the debugger in the controller (Should be bool[16]):
enter image description here

I tested the same thing with an array of integers using Html.EditorFor, and it worked just fine.
Thanks!

UPDATE:
So what I ended up doing is setting those hidden fields like this:

@Html.CheckBoxFor(model => model.NetworkSettings.ActiveSettings[0]) val0<br />
@Html.Hidden("NetworkSettings.ActiveSettings[0]",false)
@Html.CheckBoxFor(model => model.NetworkSettings.ActiveSettings[1]) val1<br />
@Html.Hidden("NetworkSettings.ActiveSettings[1]",false)

etc...
Here's a weird note though: I have 16 checkboxes, all I had to do was set hidden fields for the first 5, and then magically all 16 of them worked.

Upvotes: 1

Views: 3642

Answers (1)

Jason Meckley
Jason Meckley

Reputation: 7591

what is the rendered html for Checkboxfor? and is the 5th check box unchecked? a checkbox is only submitted in the form collection if the value is true. if the value is false it's just not sent. MS MVC requires a non-breaking zero-based index of values for an array.

So if options 1-4 are checked, option 5 is unchecked and options 6-16 are checked only 1-4 hydrated into the view model. 5 is not part of the forms collection and 6-16 are ignored.

to resolve this a hidden field with the same name as the checkbox needs to be submitted as well. the hidden field has a value of false. The modelbinder will use the checkbox value first. if it's not present the default value of false was submitted and will be used. the markup looks like this

<input type="checkbox" id="networksettings_activesettings_4" name="networksettings.activesettings[4]" value="true" />
<input type="hidden" id="networksettings_activesettings_4_hidden" name="networksettings.activesettings[4]" value="false" />

Upvotes: 2

Related Questions