0

I know how to map individual checkboxes into MVC ViewModel like:

  @Html.LabelFor(model => model.OnlyThisNetwork)
     @Html.CheckBoxFor(model => model.OnlyThisNetwork)and 

[DisplayName("Only this network")]
        public bool OnlyThisNetwork { get; set; }

But what about this table of checkboxes? I can't figure out to do it.

<table>
<thead>
                            <tr>
                                <th class="add">Frequency/Format</th>
                                <th>All</th>
                                <th>Email</th>
                                <th>Mobile</th>
                                <th>Social</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td class="add">As it happens</td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                            </tr>
                            <tr>
                                <td class="add">Once a week</td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                            </tr>
                            <tr>
                                <td class="add">Once a month</td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                                <td><input type="checkbox" title="bike" id="social" class="checkbox"></td>
                            </tr>
                        </tbody>
                    </table>

Can anyone help? I changed the post to reflect the MVC code I am binding to.

user2471435
  • 1,644
  • 7
  • 35
  • 62
  • Your first example wouldn't bind correctly and it seems to me that you want radios instead of checkboxes. – jamesSampica Aug 15 '14 at 16:20
  • The first example is working code. As for this, the customer wants checkboxes just like the supllied HTML. – user2471435 Aug 15 '14 at 16:24
  • Well now you've edited the code to work. And what would you expect to happen when someone selects "All" "As it happens" and "Email" "Once a week"? The customer probably doesn't understand the difference between a radio and checkbox. – jamesSampica Aug 15 '14 at 16:30

1 Answers1

0

Use the same name attribute for all checkboxes and set the value attribute for each. Then you'll get an array with the selected values:

<input type='checkbox' name='items' value='item1' />
<input type='checkbox' name='items' value='item2' />
<input type='checkbox' name='items' value='item3' />

In the controller you can have the post method getting an array of strings (you can have other types if the values can be converted with the actual model binder):

[HttpPost]
public ActionResult CheckItems(String[] items)
{
    ...use the values...
}

You can have the array in your model object and it will be used (if the name corresponds with the property name of the array).

Carles Company
  • 7,118
  • 5
  • 49
  • 75