How to post back selected checkboxes to Controller

Posted by TonyP on Stack Overflow See other posts from Stack Overflow or by TonyP
Published on 2010-04-03T12:36:10Z Indexed on 2010/04/03 12:43 UTC
Read the original article Hit count: 205

Filed under:
|

I have following view

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
   <h2>Tables <%=ViewData["RetriverName"] %></h2>
   <%using (Html.BeginForm("ResfreshSelectedTables", "Home"))
     { // begin form%>
   <table id="MyTable">
      <thread>
      <tr>
      <th style="width: 150px; text-align:center"><input type="checkbox" id="SelectAll" />Select All..</th>
      </tr>
         <tr>
            <th style="width:20px; text-align:right">ID</th>
            <th style="width:40px">Base Table</th>
            <th style="width:50px">Table</th>
            <th style="width:280px">Description</th>
         </tr>           
     </thread>
      <tbody>
         <%  int i = 0;
             foreach (var item in Model)
             { %>
         <tr id="row<%= i.ToString() %>">
            <td align="center" style="padding: 0 0 0 0"> 
                            <%= Html.CheckBox("selections[" + i.ToString() + "].IsSelected", item.IsSelected)%> 
                            <%= Html.Hidden("selections[" + i.ToString() + "].ID", item.id)%> 
                           <%= Html.Hidden("selections[" + i.ToString() + "].BaseTable", item.baseTable)%> 
                            <%= Html.Hidden("selections[" + i.ToString() + "].Name", item.NAME)%> 
                        </td> 

            <td style="text-align:right"><%=Html.Encode(item.id)%></td>
            <td><%= Html.Encode(item.baseTable)%></td>
            <td><%=Html.Encode(item.NAME)%></td>
            <td><%=Html.Encode(item.Description) %></td>
         </tr>
         <% i++;
             } %>
      </tbody>
   </table>
   <p>
   <input type="submit" value="saving"  />
   </p>
   <% }//end form %>
   <script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> 

    <script type="text/javascript">
       // Select All Checkboxes
       $(document).ready(function() {
          $('#SelectAll').click(function() {

             var newValue = this.checked;
             $('input:checkbox').not('input:hidden').each(function() {
               // alert(this.id+newValue );
                this.checked = newValue;
             });
          });
       }); 
    </script> 

</asp:Content>

How do I postback selected checkboxes to the controller ?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about asp.net-mvc