2 回答

TA貢獻1856條經驗 獲得超17個贊
因為您的name(and id) 屬性包括集合索引器,所以您可以使用 jQuery Attribute Ends With Selector (例如$('input[type="checkbox"][name$="Notify"]')),但這會更容易使用復選框的類名。
@Html.CheckBoxFor(m => m.EmployeeNotification[i].Notify, new { @class = "notify" })
然后你的腳本可以
// cache for performance
var checkboxes = $('.notify');
var total = checkboxes.length;
var checkall = $('#checkAll');
checkall.change(function() {
checkboxes.prop('checked', $(this).is(':checked'));
})
checkboxes.change(function() {
var count = checkboxes.filter(':checked').length;
checkall.prop('checked', (count == total));
})
但是,您的代碼還有另一個問題。默認情況下,DefaultModelBinder所需的集合索引器從零開始且連續(xù)。如果您的集合中的第一項滿足@if (Model.EmployeeNotification[i].NotifiedOn >= DateTime.Parse("2000-01-01 12:00:00 AM"))條件,那么您的EmployeeNotification屬性將null在 POST 方法中?;蛘?,如果說第 3 項滿足該條件,EmployeeNotification則將僅包含前 2 條記錄。您需要為集合索引器添加額外的輸入以允許DefaultModelBinder綁定非零/非連續(xù)索引器
@if (Model.EmployeeNotification[i].NotifiedOn >= DateTime.Parse("2000-01-01 12:00:00 AM"))
{
@Html.DisplayFor(m => m.EmployeeNotification[i].Notify)
}
else
{
@Html.HiddenFor(m => m.EmployeeNotification[i].NotificationID)
@Html.HiddenFor(m => m.EmployeeNotification[i].EmployeeID)
@Html.HiddenFor(m => m.EmployeeNotification[i].EmployeeName)
@Html.CheckBoxFor(m => m.EmployeeNotification[i].Notify, new { @class = "notify" })
<input type="hidden" name="EmployeeNotification.Index" value="@i" /> // add this
}
此外,我建議您刪除除 ID 屬性(我假設為NotificationID)之外的其他隱藏輸入。它只是包括不必要的 html,因為保存數據不應該需要這些屬性,它只是允許惡意用戶更改這些值。我還建議您的視圖模型包含一個(比如說)bool IsEditable屬性,并且當您將數據模型映射到視圖模型時,您根據 GET 方法中的條件設置該值,這樣if塊就變成了@if (Model.EmployeeNotification[i].IsEditable) { ... } else { ... }

TA貢獻1828條經驗 獲得超13個贊
使用他的建議的一些靈感,我還設法找到了上面斯蒂芬帖子的另一個解決方案。不是很干凈,但是,嘿,它有效。我絕對推薦斯蒂芬的代碼。更加簡潔明了。
將自定義 cb_Notify 復選框移動并隱藏到單元格頂部,以便在每一行中創(chuàng)建它。這將使其可搜索所有行等,我可以在循環(huán)中使用它:
@Html.CheckBox("cb_Notify", Model.EmployeeNotification[i].Notify, new { type = "hidden" })
@*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.CheckBoxFor(modelItem => Model.EmployeeNotification[i].Notify, new { @class = "notify" })
}
然后對于 jquery,使用隱藏的復選框循環(huán)遍歷所有行并設置需要設置的值:
// cache for performance
var checkAll = $('#checkAll');
checkAll.click(function () {
var maxCount = $("input[id='cb_Notify']").length;
var loopCounter;
var customer;
for (loopCounter = 0; loopCounter < maxCount; loopCounter++) {
customer = $("input[name='EmployeeNotification[" + loopCounter + "].EmployeeName']").val();
if (customer != null) {
$("input[name='EmployeeNotification[" + loopCounter + "].Notify']").prop("checked", this.checked);
}
}
toggleSetAllCheckBoxStates();
})
我創(chuàng)建的 toggleSetAllCheckBoxStates() 函數是因為有次要要求(超出了這個問題的范圍,所以我將把它排除在外)根據數據設置僅選擇員工數據的子集。但它遵循與上述示例相同的路線。
這樣,在回發(fā)事件中,我過濾掉了空數據,因為已發(fā)送通知的數據不包含 EmployeeID。其余數據保存成功。
- 2 回答
- 0 關注
- 174 瀏覽
添加回答
舉報