如何在ASP MVC中显示sweetAlert验证删除对话框

问题描述 投票:1回答:1

我正在使用SQL Server开发ASP MVC 5 Web应用程序。我试图通过具有javascript功能的按钮删除配置文件(数据库中的客户端)。

单击该按钮,将删除该配置文件,但不会显示验证对话框。我不知道原因是什么,但我怀疑javascript代码存在问题。

我还尝试创建另一个没有提交的按钮,以验证sweetAlert进程。它运作良好。

这是我的观点:

    @using (Html.BeginForm("Delete", "Client", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
 <input type="submit" value="Delete" class="btn btn-default" onclick="remove()">
    }
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script type="text/javascript">function remove() {
    swal({
        title: "Are you sure?",
        text: "Once deleted, you will not be able to recover your profile!",
        icon: "warning",
        buttons: true,
        dangerMode: true,
    })
.then((willDelete) => {
    if (willDelete) {
        swal("Poof! Your profile has been deleted!", {
            icon: "success",
        });
    } else {
        swal("Your profil is safe!");
    }
});
}
  </script>

我的控制器:

[HttpPost]
    public ActionResult Delete(int id, Client cmodel)
    {
        try
        {
            ClientManagement cdb = new ClientManagement();
            if (cdb.DeleteClient(id))
            {
                TempData["Message"]= "Client Deleted Successfully";
            }
            Session.Abandon();
            return RedirectToAction("Index", "Home");


        }
        catch
        {
            return View();
        }
    }

更新:查看:

    <input type="button" value="Delete" class="btn btn-default" onclick="remove()">
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script type="text/javascript">
    function remove() {
        swal({
            title: "Are you sure?",
            text: "Once deleted, you will not be able to recover your profile!",
            icon: "warning",
            buttons: true,
            dangerMode: true,
        })
    .then((willDelete) => {
        if (willDelete) {
            $.ajax({
                type: "POST",
                url: "/Client/Delete",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    swal("Poof! Your profile has been deleted!", {
                        icon: "success",
                    }).then(function () {
                        window.location.href = "/Home/Index"
                    });

                },
                failure: function (response) {
                    alert(response.responseText);
                },
                error: function (response) {
                    alert(response.responseText);
                }
            });

        }
        else {
            swal("Your profil is safe!");
        }
    });
    }
  </script>
javascript asp.net-mvc sweetalert
1个回答
0
投票

您正在尝试更新onclick默认行为,但您没有在javascript代码中使用preventDefault。

event.preventDefault()
© www.soinside.com 2019 - 2024. All rights reserved.