snapper snapper - 2 years ago 195
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">
<th>@Html.CheckBox("CheckAll", false, new { id = "select_all" })</th>

@for (int i = 0; i < Model.additionalCustomerInfoListView.Count; i++)
<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