ASP.Net MVC List of Checkboxes

旧城冷巷雨未停 提交于 2019-12-11 02:09:03

问题


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


回答1:


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

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



回答2:


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();
                    %>


来源:https://stackoverflow.com/questions/4731345/asp-net-mvc-list-of-checkboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!