我正在开发一个 MVC 5 Web 应用程序。在我的一个 Razor View 中,我有一张表,其中显示了几行数据。每行数据旁边都有一个删除按钮。当用户单击删除按钮时,我希望弹出 Bootstrap Modal 并要求用户确认删除。
@foreach (var item in Model.Data) {
<tr>
<td>...</td>
<td>@Html.ActionLink("Delete", "Delete", new { id = item.ID }, new { @class = "btn btn-danger btn-xs", data_toggle = "modal", data_target = "#myModal" })</td>
</tr>
}
事实上,当用户单击“删除”按钮时,模态框会弹出,但我似乎无法将 Actionlink 参数中的 ID 传递给模态框内的“确认”按钮,以便将其发送到我的 Controller 中的删除操作。
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Delete Nomination</h4>
</div>
<div class="modal-body">
Are you sure you wish to delete this nomination?
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" id="mySubmit" class="btn btn-primary">Confirm</button>
</div>
</div>
</div>
</div>
有人可以帮忙吗?
谢谢。
最佳答案
<script type="text/javascript">
//Everytime we press delete in the table row
$('.delete').click(function(e) {
e.preventDefault();
//Update the item to delete id so our model knows which one to delete
var id = $(this).data('id');
$('#item-to-delete').val(id);
});
//Everytime we press sumbit on the modal form...
$('#mySubmit').click(function() {
//Get the id to delete from the hidden field
var id = $('#item-to-delete').val();
//Call our delete actionresult and pass over this id
$.post(@Url.Action("Delete", "Delete"), { id : id } , function (data) {
alert("Deleted");
});
});
</script>
还有你的 html...
@Html.Hidden("item-to-delete", "", new { @id = "item-to-delete"})
@foreach (var item in Model.Data) {
<tr>
<td>...</td>
<td><a href="" class="btn btn-danger btn-xs delete" data-toggle= "modal" data-target="#myModal" data-id="@item.id">Delete</a></td>
</tr>
}
我认为你的 Controller 操作是这样的......
public ActionResult Delete(Guid id)
{
}
关于asp.net-mvc - MVC Actionlink 和 Bootstrap 模态提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23831745/