我有一个@ Ajax.BeginForm为我的模型有一个布尔值(@ Html.CheckBoxFor)。如果这被检查,我希望我的HttpPost动作重定向到一个新的页面。否则我希望它继续是一个@ Ajax.BeginForm并更新页面的一部分。
这是我的HttpPost动作(注意:Checkout是我的模型中的布尔值)
控制器:
[HttpPost] public ActionResult UpdateModel(BasketModel model) { if (model.Checkout) { // I want it to redirect to a new page return RedirectToAction("Checkout"); } else { return PartialView("_Updated"); } }
解决方法
您可以使用JSON并在客户端上执行重定向:
[HttpPost] public ActionResult UpdateModel(BasketModel model) { if (model.Checkout) { // return to the client the url to redirect to return Json(new { url = Url.Action("Checkout") }); } else { return PartialView("_Updated"); } }
接着:
@using (Ajax.BeginForm("UpdateModel","MyController",new AjaxOptions { OnSuccess = "onSuccess",UpdateTargetId = "foo" })) { ... }
最后:
var onSuccess = function(result) { if (result.url) { // if the server returned a JSON object containing an url // property we redirect the browser to that url window.location.href = result.url; } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。