tags:

views:

49

answers:

1

Given the code below within which I render a bunch of checkboxes in my view and the code for controller, someone please explain how can I get the values of the checkboxes (I need the key and the checked status) in the controller.

 <% foreach (string mappingId in Model.Mappings) {%>
     <tr><td>
     <%=mappingId %><br />
     <%=Html.Label("Checkbox_" + mappingId, "Sync?")%>
     <%=Html.CheckBox("Checkbox_" + mappingId, true) %>
     </td></tr>
    <% } %>

...

    [HttpPost]
    public ActionResult Sync(FormCollection collection)
    {
     foreach (var posted in collection)
     { 
      // here the "posted" variable shows up in the debugger as
      // "Checkbox_AD0D1" as Value (AD0D1 being the key in my model) and of type "object"
      // of course, this line fails but it shows what I want to do
      bool currentCheckbox = (bool) posted;
     }
     return View();
    }
A: 

See this article for info on rendering lists into HTML forms, and binding them back to controller parameters.

Drew Noakes