MVC控制器返回重定向操作

注意:在使用Ajax请求后台时是不能在后台重定向的!

解决方案:

  

if (userInfoService.CheckUser(username, psd, out msg))
            {
                return Json(new { res = 1, msg = "/UserInfo/Index" });
            }
            else {
                return Json(new { res = 0, msg = msg });
            }

 

前台接收后使用Window.location.href重定向

function (data) {
            if (data.res == 1) {
                window.location.href = data.msg;
            } else {
                $(".error_msg").html(data.msg);
            }

以下是MVC提供重定向的方法

 

Return  Redirect();方法

        public ActionResult DeleteUser(int id)
        {
            userInfoService.DeleteById(id);
            return Redirect("/UserInfo/Index");
        }

Response.Redirect();方法

        public ActionResult Index()  
        {  
            ViewData["Message"] = "欢迎使用 ASP.NET MVC!";  
            Response.Redirect("User/News");  
            return View();  
        }  

 

posted @ 2017-10-19 00:10  吃饭睡觉打豆豆o  阅读(685)  评论(0编辑  收藏  举报