【发布时间】:2021-04-30 19:58:06
【问题描述】:
这个问题几乎是八年前发布的(问题 16853364)。当我遵循提供的答案时,代码运行但记录 ID 的值没有传递给事件处理程序。我正在使用 ASP.NET Core 3.1 创建 Razor Page 应用程序。我假设最初的问题是基于使用 ASP.NET 2.x 和 MVC,希望这就是它不适合我的原因。
“索引”页面是已分配给用户的任务列表。我在页面上添加了一个复选框;完成后,用户选中复选框。选中该框应触发一个事件处理程序,该处理程序将更新数据库中的列 (tinyInt)。选中复选框确实会调用事件处理程序,但是在调用处理程序之前应该获取记录 ID 的代码没有获取 Id。我在 Index.cshtml 上显示记录并调用处理程序的代码是:
...
@if (Model.ScheduleOut.Count() == 0)
{
<p>
This speaker has not been scheduled in the past six weeks or in the next 6 months...
</p>
}
else
{
<table class="table table-striped border" height="40">
<tr class="table-secondary">
<th>
@Html.LabelFor(m => m.SchedOutgoingVM.ScheduleOutObj, "Id:")
</th>
<th>
@Html.LabelFor(m => m.SchedOutgoingVM.ScheduleOutObj, "DOT:")
</th>
<th>
@Html.LabelFor(m => m.SchedOutgoingVM.ScheduleOutObj, "Talk #'s:")
</th>
<th>
@Html.DisplayNameFor(m => m.SchedOutgoingVM.CongObj.CongName)
</th>
<th>
@Html.LabelFor(m => m.SchedOutgoingVM, "Date / Time:")
</th>
<th>
@Html.LabelFor(m => m.SchedOutgoingVM, "Talk Coordinator:")
</th>
<th>
@Html.LabelFor(m => m.SchedOutgoingVM, "TC's Phone:")
</th>
<th></th>
<th></th>
</tr>
<!-- *************** display records ***************-->
@foreach (var item in Model.ScheduleOut)
{
<tr id="1">
<td class="tdBorder">
@Html.DisplayFor(m => item.Id)
</td>
<td>
@Html.DisplayFor(m => item.DOT)
</td>
<td>
@Html.DisplayFor(m => item.SpkTalkNum)
</td>
<td>
@Html.DisplayFor(m => item.Congregation.CongName)
</td>
<td>
@Html.DisplayFor(m => item.Congregation.MtgDay) / @Html.DisplayFor(m => item.Congregation.MtgTime)
</td>
<td>
@Html.DisplayFor(m => item.Congregation.tcFirstName) @Html.DisplayFor(m => item.Congregation.tcLastName)
</td>
<td>
@Html.DisplayFor(m => item.Congregation.tcMobilePhone)
</td>
<td>
<!-- check or uncheck checkbox based on value in database -->
@if (item.Accepted == null || item.Accepted == 0)
{
<!-- if false in the database-->
<input asp-for="AcceptedBoolean" type="checkbox" onclick="ckBox(this)" form-check-input">
}
else
{
<!-- if true in the database-->
<input asp-for="AcceptedBoolean" type="checkbox" checked="checked" onclick="ckBox(this)" form-check-input">
}
</td>
</tr>
}
</table>
}
...
Index.cshtml.cs
...
//********** OnPost - update Accepted (tinyInt)column in database (using id) **********
public async Task<JsonResult> OnGetUpDateAccepted(int id)
{
///code to update database
return new JsonResult(id);
}
...
非常感谢任何关于我做错或遗漏的建议。如果您有更好的更新数据库的方法,我也想知道。
Javascript 代码:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function ckBox(e) {
if (e.checked) {
////var Testt = $(this).closest('tr').attr('Testtid');
////alert('Got this far...')
var id = $(this).closest('tr').attr('id');
console.log(Testt); //used to capture the value at this point
alert(id);
$.getJSON('?handler=UpDateAccepted&Testt=' + id, (data) => {
});
} else {
var Testt = 0
alert('bye');
$.getJSON('?handler=UpDateAccepted&Testt=' + id, (data) => {
});
}
}
</script>
【问题讨论】:
-
js代码添加到原帖底部
标签: c# asp.net-core asp.net-ajax