views:

216

answers:

1

I have a hard time in asp.net MVC2 trying to get the checked values of different checkbox.

Here is my view

    <div id="RoleSelection">
    <ul>
        <% foreach (var roles in Model.Roles)
           { 
        %>
        <li>
            <input type="checkbox" name="roles" value="<%: roles %>" /> <%: roles %>
        </li>
        <%   
            }
        %>
    </ul>
</div>

My model:

    [LocalizedDisplayName("Role", NameResourceType = typeof(UserResources))]
    public string Role { get; set; }

    public IEnumerable<string> Roles { get; set; }

So basically here I'm trying to figure out how to get all the checked checkbox from my form!

Thank you

A: 

Use the Name attribute instead of the id attribute. The id must be unique amongst all element.

The name attribute in your case will allow you to regroup the multiple checkbox into a single group.

<input type="checkbox" name="roles" value="<%: roles %>" /> <%: roles %>
Pierre-Alain Vigeant