Ann L.
Ann L.

Reputation: 13965

Action generating partial view realizes whole page must be replaced. How to redirect?

This is a partially hypothetical question, as I don't actually need to do this at the moment. I'm just anticipating that I might.

My MVC3 app has a page that has a partial view that is refreshed using MVC3 Ajax and AjaxHelper.BeginForm(). It all works nicely.

But, it's possible for the action that generates the partial view to realize that rather than see a refresh of the partial view, the user needs to be sent to a completely different page. In other words, rather than returning a PartialViewResult, the action method needs to return a RedirectResult or a regular View.

This, as you no doubt know, won't work: attempts to return a RedirectResult when the signature calls for a PartialViewResult won't compile.

So how can this be accomplished? Let's say for the sake of argument that the necessary decision can only be made server-side and only after the information in the partial view is posted back to the server.

Upvotes: 1

Views: 461

Answers (1)

Michael Edenfield
Michael Edenfield

Reputation: 28338

First, define your method to return an ActionResult, which will allow you to return either a PartialViewResult or RedirectResult, as needed.

Second, supply an OnFailure callback function in your BeginForm() method call. If the result of the AJAX call is not an HTTP 200, the OnFailure callback will be run, which you can then use to parse the redirect response and perform the redirect.

Upvotes: 1

Related Questions