C#、MVC 确认删除对话框

C#, MVC Confirmation Delete Dialog

一开始我很抱歉我的英语不好。我使用 Entity Framework 使用 MVC 创建网络应用程序。当用户尝试从 table 中删除记录时,我想创建一个自定义的确认对话框。当我使用标准确认对话框 "return confirm('Are you sure?')" 时确认有效。但我想使用自定义的确认对话框,如来自 bootstrap 的甜蜜警报或类似的东西。

控制器中的删除方法:

        [HttpPost]
    [ValidateAntiForgeryToken]
    [Authorize]
    public ActionResult Delete(int id)
    {
        DiabeticControl diabeticControl = db.DiabeticControls.Find(id);
        db.DiabeticControls.Remove(diabeticControl);
        db.SaveChanges();
        return RedirectToAction("Index");


    }

Table 在视图中:

 @foreach (var item in Model)
{
        <tr>
            <td>@Html.DisplayFor(modelItem => item.Result)</td>
            <td>@Html.DisplayFor(modelItem => item.Time)</td>
            <td>
                <div class="form-group">
                    @using (Html.BeginForm("Delete", "DiabeticControls",  new { id = item.Id }))
                    {
                            @Html.AntiForgeryToken()
                            @Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "btn btn-default xs-margin" })
                            @Html.ActionLink("Details", "Details", new { id = item.Id }, new { @class = "btn btn-default xs-margin" })
                            <button type="submit" class="btn btn-danger xs-margin delete" >Delete</button>

                    }
                </div>
                @{
                    ViewBag.Key = item.Id;
                 }


            </td>

            <td>@Html.HiddenFor(modelItem => item.UserId)</td>
        </tr>
    }

我在 中发现了一个简单的问题,但它在我的项目中不起作用。

还有我的JavaScript:

@section 脚本{

<script type="text/javascript">

    $(function () {

        $('.delete').on('click', function (e, data) {
            if (!data) {
                handleDelete(e, 1);
            } else {
                window.location = $(this).attr("@Url.Action("DiabeticControls")");
            }
        });

    });


    function handleDelete(e, stop) {
        if (stop) {
            e.preventDefault();
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover the delaer again!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete!",
                closeOnConfirm: false
            },
            function (isConfirm) {
                if (isConfirm) {
                    swal("Deleted", "", "success");
                    $('.delete').trigger('click', {});
                }
                else {
                    swal("Cancelled", "", "error");
                }
            });
        }
    };





</script>

}

好的,非常感谢您的帮助,但我找到了解决方案。问题出在我附加的库中。我将其替换为不同版本的 jquery.js、sweetalert.css 和 sweetalert.js,并根据此 post 的解决方案自定义了我的 JavaScript:

我的JavaScript:

@section 脚本{

<script type="text/javascript">

    $(document).ready(function () {

        $('.delete').on('click', function (e, data) {
            if (!data) {
                handleDelete(e, 1);
            } else {
                window.location = $(this).attr('href');
            }
        });



    });


    function handleDelete(e, stop) {
        if (stop) {
            e.preventDefault();
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover the delaer again!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "Yes, delete!",
                closeOnConfirm: false
            },
            function (isConfirm) {
                if (isConfirm) {
                    $(e.target).trigger('click', {});
                }
            });
        }
    };





</script>

}