snapper snapper - 1 year ago 146
C# Question

@Html.Checkbox select all

So I have a table inside of my view and I have gotten to the point where my select-all checkbox is selecting/unselecting. However I can't seem to get it to select all the checkboxes. It only selects the first checkbox in the list.

enter image description here

Here is a snippet from my view.

<table class="table table-bordered">
<tr>
<th>@Html.CheckBox("CheckAll", false, new { id = "select_all" })</th>

@for (int i = 0; i < Model.additionalCustomerInfoListView.Count; i++)
{
<tr>
<td>@Html.CheckBoxFor(model => model.additionalCustomerInfoListView[i].IsSelected, new { id = "someDivId" })</td>


and here is my jQuery

$('#select_all').click(function () {
var c = this.checked;
$('#someDivId').prop('checked', c)
});


Appreciate any help I can get!

Answer Source

Change new { id = "someDivId" } to new { @class= "someClass"}.

Then Select all elements with "someClass" by using:

$('#select_all').click(function () {
    $('.someClass').prop('checked', this.checked)
});  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download