如何将所选复选框发回Controller

时间:2010-04-03 12:36:10

标签: asp.net-mvc asp.net-mvc-2

我有以下观点

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

如何将所选复选框回发到控制器?

1 个答案:

答案 0 :(得分:1)

尝试为所选项目的索引添加隐藏字段,如Phil Haack的blog帖子中所示。然后,您将在控制器上将该集合作为该类型的列表接收。这将确保集合在服务器端以正确的顺序获取包含已选中和未选中复选框的项目。过滤列表,只选择IsSelected值为true的人。

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