【问题标题】:How to create bootstrap 4 confirmation modal for delete in ASP.NET MVC如何在 ASP.NET MVC 中创建用于删除的 bootstrap 4 确认模式
【发布时间】: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">&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>

最后,这是我的简单 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


【解决方案1】:

我能够重现您的问题,并找到了确认模式弹出工作所需的一些东西。

假设Delete动作方法存在:

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

    return View();
}

以下是关键点:

1) 将data-id 属性添加到ActionLink 方法。

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

2) 添加一个隐藏字段,该字段存储StudentId 的值以删除。

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

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) 在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');
});

现场示例:.NET Fiddle

类似问题:

MVC Actionlink & Bootstrap Modal Submit

bootstrap modal for delete confirmation mvc

【讨论】:

    【解决方案2】:

    如果您已经通过实体框架在控制器中设置了删除操作,那么当您添加带有操作的控制器时,应该不会很复杂,因为在用户确认删除后您要做的就是重定向到通过使用简单的 JavaScript 代码和一个隐藏字段来删除操作视图来保存项目 ID,以便将其与 URL 字符串一起传递。

    引导对话框模式

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

    用于保存要删除的项目 ID 的隐藏字段

    确保将其放在foreach 循环内

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

    Jquery 简单代码重定向到包含项目 ID 的删除操作

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

    【讨论】:

      【解决方案3】:

      上面的答案是正确且简单的,但是jquery代码应该是这样的:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 2014-07-21
        • 2015-09-29
        • 1970-01-01
        • 2017-01-19
        • 2014-03-23
        相关资源
        最近更新 更多