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

4

19

Why is the CheckBoxList removed from ASP.NET MVC preview release 5?

Currently I don't see any way in which I can create a list of checkboxes (with similar names but different id's) so people can select 0-1-more options from the list.

There is an CheckBoxList list present in the MVCContrib library, but it is deprecated. I can understand this for the other HtmlHelpers, but there does not seem to be a replacement for the CheckBoxList in preview 5.

I would like to create a very simple list like you see below, but what is the best way to do this using ASP.NET MVC preview release 5?

<INPUT TYPE="checkbox" NAME="Inhoud" VALUE="goed"> goed
<INPUT TYPE="checkbox" NAME="Inhoud" VALUE="redelijk"> redelijk
<INPUT TYPE="checkbox" NAME="Inhoud" VALUE="matig"> matig
<INPUT TYPE="checkbox" NAME="Inhoud" VALUE="slecht"> slecht
Pacian answered 14/10, 2008 at 14:29 Comment(3)
For the cleanest, hassle free solution, an answer to Stack Overflow question Maintain state of a dynamic list of checkboxes in ASP.NET MVC works well. I agree with the first answer too, I wouldn't touch Html.CheckBox. It creates more problems than it solves.Diffusion
u can use the one from here: awesome.codeplex.comBabbage
Related: Maintain state of a dynamic list of checkboxes in ASP.NET MVCIncurious
I
19

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)

Immedicable answered 14/10, 2008 at 14:35 Comment(1)
I am getting error while this as can not convert ViewData to generic lists (List)...why should this?Infantile
M
7

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

Markson answered 11/11, 2008 at 2:37 Comment(0)
R
1

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.

Ritornello answered 5/7, 2009 at 4:27 Comment(0)
C
0

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.

Childish answered 1/4, 2009 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.