/ / Come eseguire il postback delle caselle di controllo selezionate sul controller: asp.net-mvc, asp.net-mvc-2

Come postare le checkbox selezionate su Controller - asp.net-mvc, asp.net-mvc-2

Ho la seguente vista

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

Come faccio a postback delle caselle di controllo selezionate sul controller?

risposte:

1 per risposta № 1

Prova ad aggiungere un campo nascosto per l'indice dell'elemento selezionato, come mostrato in uno dei "s" di Phil Haack blog post.Si riceverà quindi la raccolta come un elenco di quel tipo sul controller. Ciò garantirà che la raccolta riceva elementi con entrambe le caselle di controllo selezionate e deselezionate, nell'ordine corretto sul lato server. Filtra l'elenco per scegliere solo quelli con IsSelected avere valore true.

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