title: Select All checkbox on EditorFor MVC C# View
body: <p>My current view is displaying a list of Employees. All rows and columns displayed are model bound.</p>
  
  <p>See my view code below:</p>
  
  <pre><code>@using System.Linq
  @using DN_.Extensions
  @model DN_.Models.NotificationsModel
  &lt;script src="~/Scripts/jquery-3.3.1.min.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript" language="javascript"&gt;
      $(function () {
          $("#checkAll").click(function () {
              $("input[id='cb_Notify']").prop("checked", this.checked).change();
              var count = $("input[name='cb_Notify']:checked").length;
          })
  
          $("input[id='cb_Notify']").click(function () {
              if ($("input[name='cb_Notify']:checked").length == $("input[id='cb_Notify']").length) {
                  $("#checkAll").prop("checked", "checked").change();
              }
              else {
                  $("#checkAll").removeProp("checked").change();
              }
          })
      })
  &lt;/script&gt;
  
  @{
      ViewBag.Title = "Link Employees";
  }
  
  &lt;h2&gt;Link Employees&lt;/h2&gt;
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
      &lt;input id="btn_Save" type="submit" value="Save" class="btn btn-default" /&gt;
  
      @Html.ActionLink("Back to List", "Index")
      &lt;p&gt;
          Select All &lt;input type="checkbox" id="checkAll" /&gt;
          Select All On Premise &lt;input type="checkbox" id="checkAllOnPremise" /&gt;
          Select All Full Timers&lt;input type="checkbox" id="checkAllFullTimers" /&gt;
      &lt;/p&gt;
      &lt;table class="table"&gt;
          &lt;tr&gt;
              &lt;th align=center&gt;Notify?&lt;/th&gt;
              &lt;th align=center&gt;Employee Name&lt;/th&gt;
              &lt;th align=center&gt;Is On Premise&lt;/th&gt;
              &lt;th align=center&gt;Is Full Time&lt;/th&gt;
              &lt;th align=center&gt;Notified On&lt;/th&gt;
          &lt;/tr&gt;
  
          @for (var i = 0; i &lt; Model.EmployeeNotification.Count; i++)
          {
  
              &lt;tr&gt;
                  &lt;td&gt;
                      @*Do not allow editing of the Notify field for employees who have been sent the notification already*@
                      @if (Model.EmployeeNotification[i].NotifiedOn &gt;= DateTime.Parse("2000-01-01 12:00:00 AM"))
                      {
                          @Html.DisplayFor(modelItem =&gt; Model.EmployeeNotification[i].Notify)
                      }
                      else
                      {
                          @*Hidden items for the post back information*@
                          @Html.HiddenFor(modelItem =&gt; Model.EmployeeNotification[i].NotificationID)
                          @Html.HiddenFor(modelItem =&gt; Model.EmployeeNotification[i].EmployeeID)
                          @Html.HiddenFor(modelItem =&gt; Model.EmployeeNotification[i].EmployeeName)
  
                          @*BELOW 3 LINES ARE THE PROBLEM DESCRIBED*@
                          @Html.EditorFor(modelItem =&gt; Model.EmployeeNotification[i].Notify)
                          @Html.CheckBoxFor(modelItem =&gt; Model.EmployeeNotification[i].Notify)
                          @*This checkbox below works with the "Select All" option, but data is not posted back.*@
                          @Html.CheckBox("cb_Notify", Model.EmployeeNotification[i].Notify)
                      }
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Model.EmployeeNotification[i].EmployeeName
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; Model.EmployeeNotification[i].IsOnPremise)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @Html.DisplayFor(modelItem =&gt; Model.EmployeeNotification[i].IsFullTime)
                  &lt;/td&gt;
                  &lt;td&gt;
                      @if (Model.EmployeeNotification[i].NotifiedOn &gt;= DateTime.Parse("2000-01-01 12:00:00 AM"))
                      {
                          @Html.RenderDate(Model.EmployeeNotification[i].NotifiedOn, "dd MMM yyyy")
                      }
                  &lt;/td&gt;
              &lt;/tr&gt;
          }
      &lt;/table&gt;
  }
  </code></pre>
  
  <p>My problem is as follows:
  I can manually select all Notify checkboxes using the first Notify Checkbox code line (i.e. using the EditorFor and CheckBoxFor option) and save the data in the post back event.
  How do I get the select All checkbox option to work on the EditorFor or CheckBoxFor model bound checkbox.</p>
  
  <p>For me the named CheckBox option works as intended with the Select All box, but I fail to be able to get the data back to the post event handler. The model data for the selected Notify column is returned as null.</p>
  
  <p>The main problem I presume is in the way the automation names the elements for the checkboxes if I look at the generated name and id element codes when debugging (sample output source code data with some of my comments added):</p>
  
  <pre><code>&lt;td&gt;
      &lt;!--Required, hidden data for post back event handler:--&gt;
      &lt;input name="EmployeeNotification[3].NotificationID" id="EmployeeNotification_3__NotificationID" type="hidden" value="6" data-val-number="The field NotificationID must be a number." data-val="true"&gt;
      &lt;input name="EmployeeNotification[3].EmployeeID" id="EmployeeNotification_3__EmployeeID" type="hidden" value="27" data-val-number="The field EmployeeID must be a number." data-val="true"&gt;
      &lt;input name="EmployeeNotification[3].EmployeeName" id="EmployeeNotification_3__EmployeeName" type="hidden" value="Charlie"&gt;
      &lt;!--@Html.EditorFor element--&gt;
      &lt;input name="EmployeeNotification[3].Notify" id="EmployeeNotification_3__Notify" type="checkbox" value="true" data-val="true" data-val-required="The Notify field is required."&gt;
      &lt;input name="EmployeeNotification[3].Notify" type="hidden" value="false"&gt;
      &lt;!--@Html.CheckBoxFor element--&gt;
      &lt;input name="EmployeeNotification[3].Notify" id="EmployeeNotification_3__Notify" type="checkbox" value="true"&gt;
      &lt;input name="EmployeeNotification[3].Notify" type="hidden" value="false"&gt;
      &lt;!--@Html.CheckBox element--&gt;
      &lt;input name="cb_Notify" id="cb_Notify" type="checkbox" value="true"&gt;
      &lt;input name="cb_Notify" type="hidden" value="false"&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>So I either need to get the Select All checkbox to work with the @Html.EditorFor or @Html.CheckBoxFor options OR I need to obtain the value set in the @Html.CheckBox in the post event handler.</p>
  
  <p>I have not been able to get the answers I need in the other, similar questions on the Select or Check All Checkboxes as they appear to use other coding languages. Please help. </p>
  
  <p><strong>Please note:</strong></p>
  
  <p>Just to put the obvious out there: The intention is to only retain 1 of the selectable Notify checkboxes in the end. The one that will work. Not all three of them. I just have them displayed for now due to my unsuccessful testing and debugging.</p>