asp.net-mvc 如何从 ASP.NET MVC 中的 JSONResult 方法重定向到控制器操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2864972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:19:26  来源:igfitidea点击:

How to redirect to a controller action from a JSONResult method in ASP.NET MVC?

asp.net-mvcredirecttoactionjsonresult

提问by ACP

I am fetching records for a user based on his UserIdas a JsonResult...

我正在根据他UserId的 JsonResult为用户获取记录...

public JsonResult GetClients(int currentPage, int pageSize)
{
   if (Session["UserId"] != "")
   {
      var clients = clirep.FindAllClients().AsQueryable();
      var count = clients.Count();
      var results = new PagedList<ClientBO>(clients, currentPage - 1, pageSize);
      var genericResult = new { Count = count, Results = results };
      return Json(genericResult);
   }
   else
   {
         //return RedirectToAction("Index","Home");
   }
}

How to redirect to a controller action from a JsonResult method in asp.net mvc?Any suggestion...

如何从 asp.net mvc 中的 JsonResult 方法重定向到控制器操作?任何建议...

EDIT:This doesn't seem to work...

编辑:这似乎不起作用......

if (Session["UserId"] != "")
            {
                var clients = clirep.FindAllClients().AsQueryable();
                var count = clients.Count();
                var results = new PagedList<ClientBO>(clients, currentPage - 1, pageSize);
                var genericResult = new { Count = count, Results = results ,isRedirect=false};
                return Json(genericResult);
            }
            else
            {
                return Json({redirectUrl = Url.Action("Index", "Home"), isRedirect = true });
            }

回答by Darin Dimitrov

This will depend on how you are invoking this controller action. As you are using JSON I suppose that you are calling it in AJAX. If this is the case you cannot redirect from the controller action. You will need to do this in the successcallback of the AJAX script. One way to achieve it is the following:

这将取决于您如何调用此控制器操作。当您使用 JSON 时,我想您是在 AJAX 中调用它的。如果是这种情况,您将无法从控制器操作重定向。您需要success在 AJAX 脚本的回调中执行此操作。实现它的一种方法如下:

return Json(new 
{ 
    redirectUrl = Url.Action("Index", "Home"), 
    isRedirect = true 
});

And in the success callback:

在成功回调中:

success: function(json) {
    if (json.isRedirect) {
        window.location.href = json.redirectUrl;
    }
}

Remark: Make sure to include isRedirect = falsein the JSON in case you don't want to redirect which is the first case in your controller action.

备注:确保包含isRedirect = false在 JSON 中,以防您不想重定向,这是控制器操作中的第一种情况。

回答by bluwater2001

Adding to Darin Dimitrov's answer. For C#.NET MVC - If you want to redirect to a different page/controller and want to send an Object/Model to the new controller, You can do something like this.

添加到 Darin Dimitrov 的答案。对于 C#.NET MVC - 如果您想重定向到不同的页面/控制器并想将对象/模型发送到新控制器,您可以执行以下操作。

In the JsonResult Method (in the controller):

在 JsonResult 方法中(在控制器中):

 ErrorModel e = new ErrorModel();
            e.ErrorTitle = "Error";
            e.ErrorHeading = "Oops ! Something went wrong.";
            e.ErrorMessage = "Unable to open Something";



return Json(new 
{ 
    redirectUrl = Url.Action("Index", "Home",e), 
    isRedirect = true 
});

And in the success callback:

在成功回调中:

success: function(json) {
    if (json.isRedirect) {
        window.location.href = json.redirectUrl;
    }
}

And if the new controller can accept the model/object like below.. you can pass the object to the new controller/page

如果新控制器可以接受如下所示的模型/对象..您可以将对象传递给新的控制器/页面

    public ActionResult Index(ErrorModel e)
    {
        return View(e);
    }

Hope this helps.

希望这可以帮助。

回答by 64X0P

What to do you think about trying to call:

你怎么想尝试打电话:

return (new YourOtherController()).JSONResultAction();

instead of using redirects?

而不是使用重定向?

回答by Mfilho_19

And if you work with areas ...

如果您与区域合作...

Controller:

控制器:

return Json(new
        {
            redirectUrl = Url.Action("Index", "/DisparadorProgSaude/", new { area = "AreaComum" }),
            isRedirect = true
        });

View:

看法:

success: function (json) {

                           if (json.isRedirect) {
                           window.location.href = json.redirectUrl;
                           }
                        },

回答by David Neale

No way to do this, the client is executing an AJAX script so will not be able to handle anything else.

没有办法做到这一点,客户端正在执行 AJAX 脚本,因此将无法处理其他任何事情。

I suggest you redirect in the client script based on the returned data in the callback function.

我建议您根据回调函数中返回的数据在客户端脚本中重定向。

Take a look at a similar question here: http://bytes.com/topic/javascript/answers/533023-ajax-redirect

在这里看看一个类似的问题:http: //bytes.com/topic/javascript/answers/533023-ajax-redirect