AJAX表单提交后重定向表单、重定向、提交后、AJAX

2023-09-11 00:37:22 作者:未央_离殇

使用MVC 4,我有一个包含一个局部视图形式的 @ Ajax.BeginForm

Using MVC 4, I have a partial view form which contains an @Ajax.BeginForm.

表单提交的如预期,结果是我的主要观点异步显示。

The form submits as expected, and the result is displayed asynchronously in my main view.

我想在我的控制器的一个条件是,如果某个参数是真的对我的表,然后将其重定向到一个全新的页面(而不是显示的结果在我的主视图)。

I want a condition on my controller that if a certain parameter is true on my form, then it redirects to a whole new page (instead of displaying the result in my main view).

当我试图返回RedirectToAction ,在表单正常显示,而不是忽略了AJAX和重定向到一个全新的页面的DIV整个视图显示。

When I tried return RedirectToAction, the whole view displays in the div that the form normally displays in, as opposed to ignoring the AJAX and redirecting to a completely new page.

有谁知道我能达致这?

推荐答案

您可以使用返回的JavaScript 来实现它。

public ActionResult MyAction()
{
    if (parameter)
    {
        return JavaScript("window.location = '" + Url.Action("Action", "Controller") + "'");
    }
    //Do something here
    return PartialView("ParitalView", Model);
}