ASP.Net MVC List of Checkboxes

Craig picture Craig · Jan 19, 2011 · Viewed 10.8k times · Source

I am passing a model to a view, which containt a List of items. A DefaultCategories has an id (int), a description (string), and a selected boolean property.

I need to list these items, with a checkbox, and check the ones where the selected property is true.

So, I was trying this:

    <h1>
        Assigned Categories</h1>
    <table>
        <%foreach (var cat in Model.DefaultCategories)
{%>
        <tr>
            <td>
                <%=cat.Category %>
            </td>
            <td>
                <%=Html.CheckBoxFor(...) %>
            </td>
        </tr>
        <%
}%>
    </table>

I'm not sure how to handle the CheckBoxFor. I will also need to query these checkboxes when the Submit is clicked....

Answer

Craig picture Craig · Jan 19, 2011

The anwer to the displaying of the Checkboxes was to simply use this:

<%=Html.CheckBoxFor(x=>cat.Selected) %>