我尝试了很多方法却没有运气......我必须离开这里。
这是我的行动链接:
@Ajax.ActionLink("Delete all", "Empty",
new AjaxOptions()
{
HttpMethod = "POST",
OnBegin = String.Format("if(!confirm('Are you sure you want to permanently delete {0} items?')){{return false;}}",ViewBag.TotalItems),
OnComplete = "location.reload()",
}
和我的ActionResult,我已经尝试了所有注释掉的回复:
public ActionResult Empty()
{
//return Json(new
//{
// redirectUrl = Url.Action("Index", "Home"),
// isRedirect = true
//});
//return RedirectToRoute(new { controller = "Home", action = "Index" });
//return Redirect("http://google.ca");
return RedirectToAction("Index", "Home");
//return new JsonResult() { Data = new { redirectURL = "/" } };
}
但我永远无法将其重定向到任何新页面:/
感谢任何帮助或指示...
编辑:我应该注意到,在最近尝试所有内容之后,我最初并没有使用“OnComplete”行。
答案 0 :(得分:1)
完成你所需要的一种粗俗方法是:
在空函数中,将RedirectToAction
替换为:
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Home");
return Json(new { Url = redirectUrl });
在您的javascript中,替换
OnComplete = "location.reload()", <-- this additional comma should be removed
到
OnSuccess= "OnAjaxSuccess"
然后在Javascript中添加一个函数
function OnAjaxSuccess(data) {
window.location.href = data.Url;
}
答案 1 :(得分:0)
你收到了回复,但你并没有按照预期得到答案。 您对服务器进行了异步调用,响应位于 OnSuccess 功能的结果参数中。
@Ajax.ActionLink("Delete all", "Empty",
new AjaxOptions()
{
HttpMethod = "POST",
OnBegin = String.Format("if(!confirm('Are you sure you want to permanently delete {0} items?')){{return false;}}",ViewBag.TotalItems),
OnComplete = "location.reload()",
OnSuccess = "Success"
}
结果就在数据参数
中<script>
function Success(data) {
console.log(data)
}
</script>
数据参数包含索引操作
返回的页面您可以尝试使用HtmlHelper而不是AjaxHelper