My current view is displaying a list of Employees. All rows and columns displayed are model bound.
See my view code below:
@using System.Linq
@using DN_.Extensions
@model DN_.Models.NotificationsModel
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script type="text/javascript" language="javascript">
$(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();
}
})
})
</script>
@{
ViewBag.Title = "Link Employees";
}
<h2>Link Employees</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<input id="btn_Save" type="submit" value="Save" class="btn btn-default" />
@Html.ActionLink("Back to List", "Index")
<p>
Select All <input type="checkbox" id="checkAll" />
Select All On Premise <input type="checkbox" id="checkAllOnPremise" />
Select All Full Timers<input type="checkbox" id="checkAllFullTimers" />
</p>
<table class="table">
<tr>
<th align=center>Notify?</th>
<th align=center>Employee Name</th>
<th align=center>Is On Premise</th>
<th align=center>Is Full Time</th>
<th align=center>Notified On</th>
</tr>
@for (var i = 0; i < Model.EmployeeNotification.Count; i++)
{
<tr>
<td>
@*Do not allow editing of the Notify field for employees who have been sent the notification already*@
@if (Model.EmployeeNotification[i].NotifiedOn >= DateTime.Parse("2000-01-01 12:00:00 AM"))
{
@Html.DisplayFor(modelItem => Model.EmployeeNotification[i].Notify)
}
else
{
@*Hidden items for the post back information*@
@Html.HiddenFor(modelItem => Model.EmployeeNotification[i].NotificationID)
@Html.HiddenFor(modelItem => Model.EmployeeNotification[i].EmployeeID)
@Html.HiddenFor(modelItem => Model.EmployeeNotification[i].EmployeeName)
@*BELOW 3 LINES ARE THE PROBLEM DESCRIBED*@
@Html.EditorFor(modelItem => Model.EmployeeNotification[i].Notify)
@Html.CheckBoxFor(modelItem => 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)
}
</td>
<td>
@Model.EmployeeNotification[i].EmployeeName
</td>
<td>
@Html.DisplayFor(modelItem => Model.EmployeeNotification[i].IsOnPremise)
</td>
<td>
@Html.DisplayFor(modelItem => Model.EmployeeNotification[i].IsFullTime)
</td>
<td>
@if (Model.EmployeeNotification[i].NotifiedOn >= DateTime.Parse("2000-01-01 12:00:00 AM"))
{
@Html.RenderDate(Model.EmployeeNotification[i].NotifiedOn, "dd MMM yyyy")
}
</td>
</tr>
}
</table>
}
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.
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.
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):
<td>
<!--Required, hidden data for post back event handler:-->
<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">
<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">
<input name="EmployeeNotification[3].EmployeeName" id="EmployeeNotification_3__EmployeeName" type="hidden" value="Charlie">
<!--@Html.EditorFor element-->
<input name="EmployeeNotification[3].Notify" id="EmployeeNotification_3__Notify" type="checkbox" value="true" data-val="true" data-val-required="The Notify field is required.">
<input name="EmployeeNotification[3].Notify" type="hidden" value="false">
<!--@Html.CheckBoxFor element-->
<input name="EmployeeNotification[3].Notify" id="EmployeeNotification_3__Notify" type="checkbox" value="true">
<input name="EmployeeNotification[3].Notify" type="hidden" value="false">
<!--@Html.CheckBox element-->
<input name="cb_Notify" id="cb_Notify" type="checkbox" value="true">
<input name="cb_Notify" type="hidden" value="false">
</td>
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.
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.
Please note:
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.