2021年1月26日星期二

Getting the id of a table row and passing it to a event handler when a checkbox is checked

This question was posted almost eight years ago, (Question 16853364). When I follow the answer that was provided the code runs but the the value of the record Id is not past to the event handler. I'm using ASP.NET Core 3.1 creating a Razor Page application. I assume the original question was based on using ASP.NET 2.x and MVC, Hopefully that is why it is not working for me.

The Index page is a list to task that a user has been assigned. I've added a checkbox to the page; after completing the user checks the checkbox. Checking the box should trigger an event handler that will update a column (tinyInt) in the database. Checking the checkbox does call the event handler, however the code that should get the record's Id before call the handler does not get the Id. My code on the Index.cshtml that displays the records and calls the handler is:

...  @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>          }    ...  

The 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);          }      ...  

Any suggestion on what I'm doing wrong or missing is greatly appreciated. If you have a better way of updating the database I would like to know that also.

https://stackoverflow.com/questions/65911427/getting-the-id-of-a-table-row-and-passing-it-to-a-event-handler-when-a-checkbox January 27, 2021 at 08:55AM

没有评论:

发表评论