开发者

How to get the checked checkbox from a ASP.NET MVC2 form

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


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 %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜