从Server for ASP.Net MVC Ajax请求重定向到新页面

我正在尝试使用RedirectToAction()从另一个控制器调用方法。 但它不起作用。 你能解释一下,我做错了什么?

 [HttpPost] public ActionResult AddToWishList(int id, bool check) { var currentUser = WebSecurity.CurrentUserId; if (currentUser != -1) { // ... } else { return RedirectToAction("Login", "Account"); } } 

我用HTML调用方法:

  $(document).ready(function () { /* call the method in case the user selects a checkbox */ $("#checkbox".concat(@Model.Id)).change(function () { $.ajax({ url: '@Url.Action("AddToWishList", "Item")', type: 'POST', data: { id: '@Model.Id', check: this.checked } }); }); }); 

它适用于我使用:

 success: function (result) { window.location.href = "@Url.Content("~/Account/Login")"; } 

但是,只有在用户未获得授权的情况下,我才需要在每次点击后导航到Login()。 你能解释一下如何在控制器中使用重定向吗?

您可以响应基本上返回JavaScriptResult的JavaScript

 [HttpPost] public ActionResult AddToWishList(int id, bool check) { return JavaScript("window.location='/Account/Login'"); } 

我看到你在ActionResult中使用[HTTPPOST]。 请检查此答案https://stackoverflow.com/a/129361/713789

请检查此答案https://stackoverflow.com/a/16643549/713789 。