我正在根据用户的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");
}
}如何从JsonResult方法重定向到asp.net mvc中的控制器操作?任何建议.
编辑:这似乎不起作用..。
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 });
}发布于 2010-05-19 11:24:04
这将取决于您如何调用此控制器操作。在使用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,这是控制器操作中的第一个情况。
发布于 2013-05-02 20:59:38
增加了达林·季米特洛夫的答案。对于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);
}希望这能有所帮助。
发布于 2011-09-27 08:02:23
你觉得该怎么打电话:
return (new YourOtherController()).JSONResultAction();而不是使用重定向?
https://stackoverflow.com/questions/2864972
复制相似问题