可以重定向到新页面的 Ajax.BeginForm [英] Ajax.BeginForm that can redirect to a new page
本文介绍了可以重定向到新页面的 Ajax.BeginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模型有一个 @Ajax.BeginForm
,它有一个布尔值 (@Html.CheckBoxFor
).如果选中此项,我希望我的 HttpPost 操作重定向到新页面.否则我希望它继续成为 @Ajax.BeginForm
并更新页面的一部分.
I have an @Ajax.BeginForm
for my model which has a boolean value (@Html.CheckBoxFor
). If this is checked, I want my HttpPost action to redirect to a new page. Otherwise I want it to just continue being an @Ajax.BeginForm
and update part of the page.
这是我的 HttpPost 操作(注意:Checkout 是我模型中的布尔值)
Here is my HttpPost action (Note: Checkout is the boolean value in my model)
控制器:
[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 并在客户端执行重定向:
You could use JSON and perform the redirect on the client:
[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;
}
}
这篇关于可以重定向到新页面的 Ajax.BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文