从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 。
- .net MVC使用数据库进行简单成员身份validation
- 使用dapper和postgresql抛出42601的“WHERE x IN y”子句:语法错误在“$ 1”处或附近
- 返回视图在控制器中使用警报脚本后
- 使用Shibboleth进行asp.net MVC身份validation
- @ Html.ValueFor(x => x.PropertyName)和@ Model.PropertyName之间的区别是什么?
- 如何使用DBContext Find方法指定预先加载
- ASP.NET MVC 5.1 C#OWIN facebook身份validation或登录请求生日,喜欢,公开个人资料,电话号码
- 如何忽略默认的EntityData属性?
- 反序列化包含单个或一组对象的JSON