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

发布于 2024-09-02 20:07:32 字数 1217 浏览 10 评论 0原文

我正在根据用户的 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");
   }
}

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时光清浅 2024-09-09 20:07:32

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

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

在成功回调中:

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

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

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.

盗琴音 2024-09-09 20:07:32

添加达林·季米特洛夫的答案。对于 C#.NET MVC - 如果您想重定向到不同的页面/控制器并希望将对象/模型发送到新控制器,您可以执行类似的操作。

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

在成功回调中:

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

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

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

希望这会有所帮助。

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.

In the JsonResult Method (in the controller):

 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.

独行侠 2024-09-09 20:07:32

您如何看待尝试调用:

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

而不是使用重定向?

What to do you think about trying to call:

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

instead of using redirects?

℉絮湮 2024-09-09 20:07:32

如果您使用区域...

控制器:

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

视图:

success: function (json) {

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

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;
                           }
                        },
假面具 2024-09-09 20:07:32

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

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文