views:

59

answers:

1

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 ?

A: 

Try adding a hidden field for the index of the selected item, as shown in one of Phil Haack's blog posts. You'd then receive the collection as a list of that type on the controller. This will ensure that the collection gets items with both the checked and unchecked checkboxes, in the correct order on the server side. Filter the list to choose only those with IsSelected having value true.

   <%= Html.Hidden("selections.Index", i) %>
tvanfosson