【发布时间】:2020-02-21 02:38:05
【问题描述】:
我正在编写一个 ASP.Net Core 2.2 应用程序,尝试实现 Bootstrap 4 模式视图以确认从数据库/表视图中删除记录,但未成功。
表格的每一行都有一个删除按钮。单击表中给定行的删除按钮时,模态删除确认框将按预期出现。当用户点击按钮确认删除记录时,模态删除确认框消失,没有任何反应。
这是生成表中行的 Razor 代码:
<tbody>
@foreach (LearningObjective item in Model.LearningObjectives)
{
<tr>
<td>@Html.DisplayFor(model => item.Sentence)</td>
<td>@Html.DisplayFor(model => item.Verbs)</td>
<td>@Html.DisplayFor(model => item.Measurables)</td>
<td>@Html.DisplayFor(model => item.Blooms)</td>
<td>@Html.DisplayFor(model => item.Levels)</td>
@if (ViewBag.Title == "Build and Analyze Learning Objectives")
{
<td>
<a id="deleteCustomerModal"
data-toggle="modal"
asp-action="DeleteLearningObjective"
asp-route-id="@item.Id"
data-target="#modal-delete"
class="btn btn-sm btn-danger">
<i class="fa fa-trash-o"></i>
Delete
</a>
</td>
}
</tr>
}
</tbody>
以下是上述代码如何呈现实际表格行的示例(来自页面源):
<tr>
<td>analyze all the fun things we can do with this app</td>
<td>Analyze, Do</td>
<td>Yes, No</td>
<td>Yes, No</td>
<td>4, -</td>
<td>
<a id="deleteCustomerModal"
data-toggle="modal"
data-target="#modal-delete"
class="btn btn-sm btn-danger"
href="/Home/DeleteLearningObjective/296">
<i class="fa fa-trash-o"></i>
Delete
</a>
</td>
</tr>
上面那个href是正确的。
这是模态表单的 HTML:
<form asp-action="DeleteLearningObjective" role="form"><
<div class="modal fade" id="modal-delete" tabindex="-1" role="dialog" aria-labelledby="modalDeleteLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="modalDeleteLabel">Delete Learning Objective</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body form-horizontal">
Are you sure you want to delete this learning objective?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-danger" id="modalDeleteButton">Delete</button>
</div>
</div>
</div>
</div>
</form>
这里是 Javascript:
$(function () {
// boostrap 4 load modal example from docs
$('#modal-delete').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget); // Button that triggered the modal
var url = button.attr("href");
var modal = $(this);
// note that this will replace the content of modal-content everytime the modal is opened
modal.find('.modal-content').load(url);
});
$('#modal-delete').on('hidden.bs.modal', function () {
// remove the bs.modal data attribute from it
$(this).removeData('bs.modal');
// and empty the modal-content element
$('#modal-delete .modal-content').empty();
});
});
单步执行:
*表格行渲染正确,每行href值正确。
*模态显示如预期。
*当模态框删除按钮被点击时,jQuery 代码执行。代码成功地从表格行中找到并提取了正确的 href 值。
*jQuery 代码的最后一行 (modal.find('.modal-content').load(url);) 执行,但我确实在 Chrome 的调试控制台中看到以下类型的错误:
获取https://localhost:44389/Home/DeleteLearningObjective/296405
*模态关闭
因此,表格行中的 href 值实际上从未传递给模式。我怀疑这是因为它被解释为 GET 请求而不是 POST 请求(根据错误)。
我已经在谷歌上搜索了两天并试图解决这个问题,我正准备用锤子敲我的电脑。
【问题讨论】:
标签: asp.net-core bootstrap-4 bootstrap-modal