我正在尝试使用另一个控制器调用方法RedirectToAction()
。但这不起作用。你能解释一下我做错了什么吗?
[HttpPost]
public ActionResult AddToWishList(int id, bool check)
{
var currentUser = WebSecurity.CurrentUserId;
if (currentUser != -1)
{
// ...
}
else
{
return RedirectToAction("Login", "Account");
}
}
我在 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
}
});
});
});
如果我使用它,它会起作用:
success: function (result) {
window.location.href = "@Url.Content("~/Account/Login")";
}
但我不需要在每次点击后导航到 Login(),除非用户未获得授权。您能解释一下如何在控制器中使用重定向吗?
您可以回复JavaScript基本上返回JavaScript结果.
[HttpPost]
public ActionResult AddToWishList(int id, bool check)
{
return JavaScript("window.location='/Account/Login'");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)