Why is it removed: ASP.NET MVC CheckBoxList (without MVCContrib)

五迷三道 提交于 2019-12-02 20:36:15
Corin Blaikie

A for loop in the view to generate the checkboxes

<% foreach(Inhoud i in ViewData["InhoudList"] as List<Inhoud>) { %>
  <input type="checkbox" name="Inhoud" value="<%= i.name %>" checked="checked" /> <%= i.name %>
<% } %>   

Don't use Html.Checkbox, as that will generate two values for each item in the list (as it uses a hidden input for false values)

I recently blogged about implementing the CheckBoxList helper in the MVC Beta. Here is the link.

Gerardo Contijoch

I have my own implementation of CheckListBox which has support for ModelState. If you are interested it's in Un CheckBoxList que funciona en ASP.NET MVC. The post is in Spanish, but you shouldn't have any problems reading the code.

What is interesting in Jeremiah solution is the fact that you can set the initial state of the checkboxes, something you can't do with my CheckListBox.

I recommend using JeremiahClark extension posted above. (CheckBoxList)

My controller resulted into very simple instructions. For clarify I add a fragment of my code that's absent in the sample.

        var rolesList = new List<CheckBoxListInfo>();
        foreach (var role in Roles.GetAllRoles())
        {
            rolesList.Add(new CheckBoxListInfo(role, role, Roles.IsUserInRole(user.UserName, role)));
        }
        ViewData["roles"] = listaRoles;

And in the view:

<div><%= Html.CheckBoxList("roles", ViewData["roles"]) %></div>

That's all.

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