我正在根据他的 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 方法重定向到 Controller 操作?任何建议...
编辑:
这似乎不起作用...
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 });
}
最佳答案
这将取决于您如何调用此 Controller 操作。当您使用 JSON 时,我想您是在 AJAX 中调用它的。如果是这种情况,您将无法从 Controller 操作重定向。您需要在 success
中执行此操作AJAX 脚本的回调。实现它的一种方法如下:
return Json(new
{
redirectUrl = Url.Action("Index", "Home"),
isRedirect = true
});
在成功回调中:
success: function(json) {
if (json.isRedirect) {
window.location.href = json.redirectUrl;
}
}
备注:确保包含
isRedirect = false
在 JSON 中,以防您不想重定向,这是 Controller 操作中的第一种情况。
关于asp.net-mvc - 如何从 ASP.NET MVC 中的 JSONResult 方法重定向到 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2864972/