views:

67

answers:

1

I am using asp.net mvc. I have generated a view that retrieve all unapproved users in the asp.net membership table. I have put checkboxes next to them for someone to bring up a view. The goal is that someone should be able to check certain checkboxes, hit save and that will go back to asp.net membership and change the IsApprove flag to true for these users.

How do i extract what fields are set as true when i am in the controller class?

here is the view code:

<% using (Html.BeginForm()) {%>
<table id="hor-zebra" border = 2>
<tr><td>User</td><td>Approve</td>
</tr>
<%
    MembershipUserCollection membership = (MembershipUserCollection)ViewData["UnapprovedUsers"];
    foreach (MembershipUser member in membership)
     {
        %><tr><td>
        <%=Html.Encode(member.UserName) %> </td><td>
        <%= Html.CheckBox("Approve:" + member.UserName, false) %>
        </td></tr>
  <%
     }
%>
</table>
<input type="submit" value="Save" /><% } %>

Here is the controller code:

    [AcceptVerbs(HttpVerbs.Post)]
    public void ApproveUsers(FormCollection formCollection)
    {
        Console.Write("I have not idea how i can determine which checkboxes are checked");
    }
+1  A: 

First of all, be careful with Html.Checkbox(). It does not just render a checkbox, but a hidden field as well. Each user (such as "username1" in this case) will have the following rendered:

<input id="Approve: username1" name="Approve: username1" type="checkbox" value="true"/>
<input name="Approve: username1" type="hidden" value="false" />

This question and selected answer discus it in more detail. One option is to just write the html yourself for this, such as:

<input type="checkbox" name="<%=member.UserName%>" />

When this gets posted to your Controller you can retrieve it by Request.Form[member.Username]. If it's null, the box wasn't checked. If it has a value of "On", it was.

Kurt Schindler