如何重定向到从ASP.NET MVC一个JSONResult方法的控制器行动? [英] How to redirect to a controller action from a JSONResult method in ASP.NET MVC?

查看:599
本文介绍了如何重定向到从ASP.NET MVC一个JSONResult方法的控制器行动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获取记录基于他的用户ID 作为JsonResult ...

用户

 公共JsonResult GetClients(INT当前页,诠释的pageSize)
{
   如果(会话[用户ID]!=)
   {
      VAR的客户= clirep.FindAllClients()AsQueryable已()。
      变种数= clients.Count();
      VAR的结果=新PagedList< ClientBO>(客户,当前是 - 1,pageSize的);
      VAR genericResult =新的{数=计,结果=结果};
      返回JSON(genericResult);
   }
   其他
   {
         //返回RedirectToAction(指数,家);
   }
}

如何重定向到在asp.net的MVC一个JsonResult方法的控制器行动?任何建议...

编辑:
这似乎并没有工作...

 如果(会话[用户ID]!=)
            {
                VAR的客户= clirep.FindAllClients()AsQueryable已()。
                变种数= clients.Count();
                VAR的结果=新PagedList< ClientBO>(客户,当前是 - 1,pageSize的);
                VAR genericResult =新的{数=计,结果=结果,isRedirect = FALSE};
                返回JSON(genericResult);
            }
            其他
            {
                返回JSON({=的redirectUrl Url.Action(指数,家),isRedirect =真});
            }


解决方案

这将取决于你如何调用该控制器操作。当你正在使用JSON我想你在AJAX调用它。如果是这种情况下,你不能从控制器动作重定向。您需要做的成功的AJAX脚本的回调。实现这一目标的一种方法如下:

 返回JSON(新
{
    的redirectUrl = Url.Action(指数,家),
    isRedirect =真
});

而在成功回调:

 成功:函数(JSON){
    如果(json.isRedirect){
        window.location.href = json.redirectUrl;
    }
}

注:确保包括 isRedirect = FALSE 在情况下,JSON你不想重定向这是在您的控制器动作尚属首例。

I am fetching records for a user based on his UserId as a 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...

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 });
            }

解决方案

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 success callback of the AJAX script. One way to achieve it is the following:

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 = false in the JSON in case you don't want to redirect which is the first case in your controller action.

这篇关于如何重定向到从ASP.NET MVC一个JSONResult方法的控制器行动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆