Pentru mine, soluția Jeremy a lucrat cea mai mare parte, dar a trebuit să -l înlocuiască .attr cu .prop. În caz contrar , o dată ce am unică făcut clic pe o casetă de selectare ar opri reacția la „maestru“ caseta de selectare.
în (pagina principală) _Layout.cshtml
$(document).ready(
manageCheckboxGroup('chkAffectCheckboxGroup', 'checkbox-group')
);
într-un .js care se face referire
function manageCheckboxGroup(masterCheckboxId, slaveCheckboxesClass) {
$("#" + masterCheckboxId).click(function () {
$("." + slaveCheckboxesClass).prop('checked', this.checked);
});
$("." + slaveCheckboxesClass).click(function () {
if (!this.checked) {
$("#" + masterCheckboxId).prop('checked', false);
}
else if ($("." + slaveCheckboxesClass).length == $("." + slaveCheckboxesClass + ":checked").length) {
$("#" + masterCheckboxId).prop('checked', true);
}
});
}
în pagina HTML (aparat de ras)
<table class="table">
<thead>
<tr>
<th><input id="chkAffectCheckboxGroup" type="checkbox" checked="checked" /></th>
<th>
@Html.DisplayNameFor(model => model.Clients[0].ClientId)
</th>
<th>
@Html.DisplayNameFor(model => model.Clients[0].Name)
</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Clients.Count(); i++)
{
<tr>
<td>
<input type="hidden" asp-for="Clients[i].Id" class="form-control" />
<input type="hidden" asp-for="Clients[i].Name" />
<input type="checkbox" class="checkbox-group" asp-for="Clients[i].Selected" />
</td>
<td>
@Html.DisplayFor(modelItem => Model.Clients[i].Id)
</td>
<td>
@Html.DisplayFor(modelItem => Model.Clients[i].Name)
</td>
</tr>
}
</tbody>
</table>
IMPORTANT: De
asemenea, la început am avut o @foreachbuclă în HTML și nu a funcționat ..., trebuie să utilizați o @for (int i = 0; i < Model.Clients.Count(); i++)buclă în loc altfel va termina cu o listă goală din OnPostAsync().