Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在MVC中使用警报框而不重定向到其他页面?_Javascript_Jquery_Asp.net_Asp.net Mvc_Razor - Fatal编程技术网

Javascript 如何在MVC中使用警报框而不重定向到其他页面?

Javascript 如何在MVC中使用警报框而不重定向到其他页面?,javascript,jquery,asp.net,asp.net-mvc,razor,Javascript,Jquery,Asp.net,Asp.net Mvc,Razor,我正在尝试显示一个自定义警报框,以确保用户想要删除某个项目。删除该项目后,将打开一个新的警报框,并显示“该项目已删除”,然后用户必须单击“确定”按钮 我的问题是,删除项目后,页面将重定向到其他页面,而不单击“确定”按钮。我如何安排代码来实现我的观点 控制器 public ActionResult Delete(int id) { Category category = db.Categories.Find(id); db.Categories.Remove(category);

我正在尝试显示一个自定义警报框,以确保用户想要删除某个项目。删除该项目后,将打开一个新的警报框,并显示“该项目已删除”,然后用户必须单击“确定”按钮

我的问题是,删除项目后,页面将重定向到其他页面,而不单击“确定”按钮。我如何安排代码来实现我的观点

控制器

public ActionResult Delete(int id)
{
   Category category = db.Categories.Find(id);
   db.Categories.Remove(category);
   db.SaveChanges();
   return RedirectToAction("Index");
}
删除按钮

<button type="button" class="btn bg-red waves-effect" onclick="Delete();">Delete</button>
删除
警报脚本

<script>
    function Delete()
    {
        swal({
            title: "Are you sure?",
            text: "You will not be able to recover this imaginary file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, delete it!",
            closeOnConfirm: false
        },
            function () {
                location.href = '@Url.Action("Delete", "Categories", new { id = Model.Id })',
                swal({
                    title: "Deleted!",
                    text: "Your imaginary file has been deleted.",
                    type: "success"
                })
            });
    }
</script>

函数Delete()
{
游泳({
标题:“你确定吗?”,
text:“您将无法恢复此虚拟文件!”,
键入:“警告”,
showCancelButton:true,
confirmButtonColor:#DD6B55“,
confirmButtonText:“是的,删除它!”,
closeOnConfirm:false
},
函数(){
location.href='@Url.Action(“删除”,“类别”,新建{id=Model.id})”,
游泳({
标题:“已删除!”,
文本:“您的虚拟文件已被删除。”,
类型:“成功”
})
});
}

当您执行行
location.href=“Categories/Delete/123”
时,浏览器基本上会将当前页面重定向到此新url,基本上会发出新的GET请求。如果您只想从服务器上删除该项并向用户显示一个警报,那么您需要通过ajax来完成

因此,在“确认删除”按钮的回调中,对服务器进行ajax调用。您可以使用jQuery
$.post
方法

function Delete() {
    swal({
            title: "Are you sure?",
            text: "You will not be able to recover this imaginary file!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes, delete it!",
            closeOnConfirm: false
        },
        function() {
            var url = '@Url.Action("Delete", "Home", new {id = Model.Id})';
            $.post(url,
                function(response) {
                    if (response.success === "success") {
                        swal({
                            title: "Deleted!",
                            text: "Your imaginary file has been deleted.",
                            type: "success"
                        });
                    } else {
                        // failed to delete. show messaage to user
                        alert(response.message);
                    }
                });
        });
}
另外,由于您是通过ajax进行Delete方法调用,因此可以返回json响应而不是重定向响应。我还建议在删除操作更改数据时将其保留为HttpPost操作

[HttpPost]
public ActionResult Delete(int id)
{
   try
   {
     Category category = db.Categories.Find(id);
     db.Categories.Remove(category);
     db.SaveChanges();
     if(Request.IsAjaxRequest())
     {
       return Json(new { status  = "success" });
     }
     return RedirectToAction("Index");
   }
   catch(Exception e)
   {
     return Json(new { status = "failed",message=e.Message });
   }
}

非常感谢你。我想答案是jQuery。现在问题解决了。