【发布时间】:2018-07-02 08:58:36
【问题描述】:
我在 ASP.NET MVC 中创建引导确认模式时遇到问题。单击视图内的删除链接时,我已成功调用模态,但是当我想确认时,没有任何反应。
索引视图()
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.CurrentGrade.GradeName)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Surname)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CurrentGrade.GradeName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Surname)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) |
@Html.ActionLink("Details", "Details", new { id=item.StudentId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.StudentId }, new { @class="element", @data_toggle = "modal", @data_target = "#exampleModalCenter" })
</td>
</tr>
}
</table>
这是我正在调用的模态:
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h6>Are you sure that you want to delete this?</h6>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-danger">Delete</button>
</div>
</div>
</div>
</div>
最后,这是我的简单 js 脚本。
$(document).ready(function () {
$('.element').click(function (e) {
$('#exampleModalCenter').modal('show');
if (confirm) {
return true;
} else {
return false;
}
});
});
更新
我尝试根据 Soham 提供的链接编辑 js 代码,但没有任何运气。
$(document).ready(function () {
$('#exampleModalCenter').on('show.bs.modal', function (e) {
$(this).find('.btn-danger').attr('href', $(e.relatedTarget).data('href'));
$('.debug-url').html('Delete URL: <strong>' + $(this).find('.btn-danger').attr('href') + '</strong>');
});
});
也许问题出在@Html.ActionLink for Delete?
@Html.ActionLink("Delete", "Delete", new { id = item.StudentId }, new { @data_toggle = "modal", @data_target = "#exampleModalCenter" })
【问题讨论】:
-
发生这种情况是因为您希望弹出窗口的行为类似于默认的 javascript 确认消息。但事实并非如此。默认值会停止执行,直到您按下其中一个按钮,而您的自定义弹出窗口没有。使用当前设置执行此操作的最简单方法是更改 click 函数以对删除操作进行 ajax 调用,而不是返回 true。
-
您可以在确认弹出窗口中的删除按钮上直接设置 GET/POST 操作。您只需要一个 ID 或一些数据来识别正在删除的行。看看这个answer。
-
if (confirm) {confirm在哪里定义? -
@Soham 谢谢队友,写下答案让我可以接受:)
标签: javascript jquery asp.net twitter-bootstrap