Imagine a scenario where I have a sub-form on a page. This sub form is contained in a partial user control and posts to its own dedicated controller action. In the event that a business rule is violated, I want to return the same view the user was previously on.
Example:
- User is at /example/page1 which renders partial "SignOnForm"
- SignOn form posts to "Accounts/SignOn".
- In the event that something is invalid, I want to return the user to /example/page1 with the model state in tact. However, I cannot hard code the view because the user might be on a different page such as /othercontroller/page10.
Best Answer
You could bring on the return URL all the way through the SignOn process, using the request query string.
First, specify the page to return to whenever you render your SignOn partial:
Use RenderAction if the current context is not the Account Controller. The feature is currently not in the MVC release, so you need to include the ASP.NET MVC's Future library in your solution.
Next, the SignOn controller:
SignOn form:
Finally, in your SignOn controller that processes the Form POST, you can return the user to the 'returnURL' using the following code: