2

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

Chandu
  • 81,493
  • 19
  • 133
  • 134
Craig
  • 18,074
  • 38
  • 147
  • 248

2 Answers2

4

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

<%=Html.CheckBoxFor(x=>cat.Selected) %>
Craig
  • 18,074
  • 38
  • 147
  • 248
1

This is to bind the checkbox to the Description. Instead of a forech, why don't you use a datagrid?

<% Html.Telerik().Grid<ModelName>(TempData[SomeList] as List<T>)
                            .Name("Grid")
                            .DataKeys(keys => { keys.Add(x => x.Id); })
                            .Columns(cols =>
                            {
                               cols.Template(o =>
                               {

                                  %>
                                   <%=Html.SecureCheckBoxFor(model => model.Description, Model.Description)
                                <%}).Title("Select");
                            })
                            .EnableCustomBinding(true)
                            .Render();
                    %>
Divi
  • 7,621
  • 13
  • 47
  • 63
  • 1
    Sorry, didn't realise that. Have a look at this, I think this is what you want. http://stackoverflow.com/questions/2409552/foreach-on-ienumerable-property-and-checkboxfor-in-asp-net-mvc – Divi Jan 19 '11 at 03:51
  • Excellent.. That link may have helped. I am using <%=Html.CheckBoxFor(x=>cat.Selected) %>, and that is indeed showing a checkbox, with the correct value. Now, I need to work out how to get the values back on the Submit. – Craig Jan 19 '11 at 04:21
  • 2
    I think this post should help you with it. http://nickstips.wordpress.com/2011/01/18/asp-net-mvc-displaying-and-retrieving-values-from-a-list-of-checkboxes/ – Divi Jan 19 '11 at 04:32