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.

3
  • Your first example wouldn't bind correctly and it seems to me that you want radios instead of checkboxes. Commented Aug 15, 2014 at 16:20
  • The first example is working code. As for this, the customer wants checkboxes just like the supllied HTML. Commented Aug 15, 2014 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. Commented Aug 15, 2014 at 16:30

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

But I don't want to use <input>. I want use Html.CheckboxFor
You can try this then: stackoverflow.com/questions/8356703/… but I think it's easier doing it using the input tags.
Use Html.Checkbox("items")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.