由于垃圾桶按钮位于
tr
,可以尝试的一件事是处理垃圾箱按钮本身的click事件,并阻止事件向上传播。我假设您使用的是引导,所以下面的解决方案使用的是jQuery。这应该适用于
button
或
ActionLink
但通常当你使用
<a>
,您要使用该锚点的
href
作为
data-target
.我决定使用
按钮
.所以在你身上
html
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#DeleteConfirmationModal">
<i class="fas fa-trash-alt"></i>
</button>
在javascript中:
$(document).ready(function() {
$(".btn.btn-danger").click(function(e) {
e.stopPropagation()
let target = $(this).data('target')
$(target).modal('show')
})
})
下面是一个演示:
https://www.bootply.com/paKZLt8L94
如何在MVC中实现
在视图中添加防伪令牌(替换
ControllerName
下面是您的
Controller
)
// The purpose of this form is to create a Anti-forgery token
// needed by the controller. Insert this anywhere in your html
@using (Html.BeginForm("DeleteConfirmed", "ControllerName", FormMethod.Post, new { id = "delete-forklift-form" }))
{
@Html.AntiForgeryToken()
<input type="hidden" id="forklift-Id" name="id" value="" />
}
在视图中的某个位置添加模式标记(可以在末尾添加)
<!-- Modal -->
<div class="modal fade" id="DeleteConfirmationModal" 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">
Are you sure you want to delete this item?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button id="delete-confirm" type="button" class="btn btn-primary">Delete</button>
</div>
</div>
</div>
</div>
将项目ID添加为
data-attribute
到您的按钮:
// We need this ID to be passed to the delete action
// This is the foreach statement in you view from the pastebin link
@foreach(var item in Model.ForkliftStockInventoryList)
{
// ... rest of the stuff
<button type="button" class="btn btn-danger" data-fid="@item.ForkliftID" data-toggle="modal" data-target="#DeleteConfirmationModal">
<i class="fas fa-trash-alt"></i>
</button>
}
将JavaScript包装在脚本块中
将修改后的JavaScript代码放在视图的末尾:
@section Scripts{
<script>
$(document).ready(function () {
// handler for the trash button
$(".btn.btn-danger").click(function (e) {
e.stopPropagation()
let target = $(this).data('target')
let forkliftId = $(this).data('fid')
// update the forklift to delete in the hidden field
$('#forklift-Id').val(forkliftId)
$(target).modal('show')
})
// handler for the delete confirmation on the modal
$('#delete-confirm').click(function () {
$('#delete-forklift-form').submit()
})
})
</script>
}
在您的
_Layout
正在使用jQuery之后呈现的视图,这就是jQuery的所有内容都不起作用的原因。推
@RenderSection("scripts", required: false)
以使布局中的所有脚本在每个视图中的脚本之前渲染。包装好
<script>...</script>
在叉车索引视图中
Scripts
块(见上文)
改变
@RenderSection("scripts", required: false)
@*<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>*@
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
到
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>
@RenderSection("scripts", required: false)
// RenderSection scripts at the end