简体   繁体   English

如何在 ASP.NET MVC 中创建用于删除的引导程序 4 确认模式

[英]How to create bootstrap 4 confirmation modal for delete in ASP.NET MVC

I'm having trouble creating bootstrap confirmation modal in ASP.NET MVC.我在 ASP.NET MVC 中创建引导程序确认模式时遇到问题。 I've managed to successfully call modal when clicking on delete link inside view, but when I want to confirm, nothing happens.单击视图内的删除链接时,我设法成功调用了模态,但是当我想确认时,没有任何反应。

Index View()索引视图()

<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>

Here is modal that I'm calling:这是我正在调用的模态:

<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">&times;</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>

And finally, here is my simple js script.最后,这是我的简单 js 脚本。

    $(document).ready(function () {
        $('.element').click(function (e) {

            $('#exampleModalCenter').modal('show');
            if (confirm) {
                return true;
            } else {
                return false;
            }
        });
});

UPDATE更新

I tried edit js code according to link that Soham provided but without any luck.我尝试根据 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>');
        });
    });

Maybe problem lies in @Html.ActionLink for Delete?也许问题出在@Html.ActionLink for Delete?

 @Html.ActionLink("Delete", "Delete", new { id = item.StudentId }, new { @data_toggle = "modal", @data_target = "#exampleModalCenter" })

I was able to reproduce your issue and found some things required to get confirm modal popup work.我能够重现您的问题,并发现获得确认模式弹出工作所需的一些东西。

Assumed Delete action method exists:假设存在Delete操作方法:

[HttpPost]
public ActionResult Delete(int id) 
{
    // other stuff

    return View();
}

Here are those key points:以下是这些要点:

1) Add data-id attribute to ActionLink method. 1) 为ActionLink方法添加data-id属性。

@Html.ActionLink("Delete", "Delete", new { id=item.StudentId }, new { @class="element", 
                 @data_toggle = "modal", @data_target = "#exampleModalCenter", 
                 @data_id = item.StudentId })

2) Add a hidden field which stores value of StudentId to delete. 2) 添加一个隐藏字段,用于存储要删除的StudentId值。

@Html.Hidden("itemid", "", new { id = "itemid" })

3) Add id element to 'Delete' button in modal popup. 3) 将id元素添加到模式弹出窗口中的“删除”按钮。

<div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    <button type="button" id="Delete" class="btn btn-danger">Delete</button>
</div>

4) Use this script inside document.ready to show modal popup and make request for 'Delete' button click: 4) 在document.ready使用此脚本来显示模式弹出窗口并请求“删除”按钮单击:

$('.element').click(function (e) {
    e.preventDefault();
    $('#exampleModalCenter').modal('show');

    var id = $(this).data('id');
    $('#itemid').val(id);
});


$('#Delete').click(function () {
   var studentId = $('#itemid').val();

   $.post(@Url.Action("Delete", "Delete"), { id: studentId }, function (data) {
       // do something after calling delete action method
       // this alert box just an example
       alert("Deleted StudentId: " + studentId);
   });

   $('#exampleModalCenter').modal('hide');
});

Live example: .NET Fiddle现场示例: .NET Fiddle

Similar issues:类似问题:

MVC Actionlink & Bootstrap Modal Submit MVC Actionlink & Bootstrap Modal 提交

bootstrap modal for delete confirmation mvc 用于删除确认 mvc 的引导模式

If you already have the delete action setup in the controller by entity framework, when you added a controller with actions, it should not be complicated, as all what you have to do after the user confirms the delete is to redirect to the delete action view by using simple JavaScript code and a hidden field to hold the item id to pass it in with the URL string.如果你已经通过实体框架在控制器中设置了删除动作,那么当你添加一个带有动作的控制器时,应该不会很复杂,因为用户确认删除后你要做的就是重定向到删除动作视图通过使用简单的 JavaScript 代码和一个隐藏字段来保存项目 id 以将其与 URL 字符串一起传递。

The bootstrap dialog modal引导对话框模式

<!-- Confirmation modal -->
<div class="modal fade" id="confirmdelete" tabindex="-1" role="dialog" aria-labelledby="confirmdelete" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="confirmdelete">Action Confirmation</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to delete this record ??</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" id="action">Delete</button>
            </div>
        </div>
    </div>
</div>

Hidden field to hold the item id to be deleted隐藏字段,用于保存要删除的项目 id

Make sure it is placed inside the foreach loop确保它被放置在foreach循环内

 @Html.HiddenFor(c => item.ID, new { @id = "hdnItemId" })

Jquery simple code to redirect to the delete action with item id included Jquery简单代码重定向到包含项目ID的删除操作

$(document).ready(function () {
  $('#action').click(function () {
    var itemId = $('#hdnItemId').val();
    var actionLink = "/Banks/Delete/" + itemId;
    window.location.href = actionLink;
  });
});

The above answer is correct and easy but jquery code is supposed to be like this:上面的答案是正确且简单的,但 jquery 代码应该是这样的:

        $(document).ready(function () {
   
         $('#action').click(function () {
          var itemId = $('#hdnItemId').val();
          var actionLink = "/MyController/MyDeleteAction?id=" + itemId;
          window.location.href = actionLink;
         });
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM