我正在尝试使用从另一个控制器调用方法RedirectToAction()。但这是行不通的。您能解释一下我在做什么错吗?
[HttpPost]
public ActionResult AddToWishList(int id, bool check)
{
var currentUser = WebSecurity.CurrentUserId;
if (currentUser != -1)
{
// ...
}
else
{
return RedirectToAction("Login", "Account");
}
}
Run Code Online (Sandbox Code Playgroud)
我在HTML中调用该方法:
<script>
$(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
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
如果我使用它会起作用:
success: function (result) {
window.location.href = "@Url.Content("~/Account/Login")";
}
Run Code Online (Sandbox Code Playgroud)
但是,只有在未授权用户的情况下,我不需要在每次单击后导航到Login()。您能否解释一下如何在控制器中使用重定向?
你可以响应的JavaScript基本上返回JavaScriptResult。
[HttpPost]
public ActionResult AddToWishList(int id, bool check)
{
return JavaScript("window.location='/Account/Login'");
}
Run Code Online (Sandbox Code Playgroud)