views:

436

answers:

1

How would one go about detecting a page refresh / F5 key push on the controller handling the postback? I need to distinguish between the user pressing one of two buttons (e.g., Next, Previous) and when the F5 / page refresh occurs.

My scenario is a single wizard page that has different content shown between each invocation of the user pressing the "Next" or "Previous" buttons. The error that I am running into is when the user refreshes the page / presses the F5 key, the browser re-sends the request back to the controller, which is handled as a post-back and the FormCollection type is used to look for the "submitButton" key and obtain its value (e.g., "Next," "Send"). This part was modeled after the post by Dylan Beattie at http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework.

Maybe I'm trying to bend MVC 2 to where it isn't meant to go but I'd like to stay with the current design in that the underlying database drives the content and order of what is shown. This allows us to add new content into the database without modifying the code the displays the content.

Thanks, Michael

+2  A: 

The recommended way is to use PRG pattern - Post-Redirect-Get. Meaning: after submitting, redirect a client to Index or what, causing only reloading Index when user hits F5, not posting the data again.

Check this: http://en.wikipedia.org/wiki/Post/Redirect/Get

And this: http://www.google.sk/search?q=prg+mvc+asp.net

František Žiačik
To put it another way... Have your controller action return a RedirectToAction result instead of a View result.
AaronSieb
Your second link provided, I found the following http://www.eworldui.net/blog/post/2008/05/ASPNET-MVC---Using-Post2c-Redirect2c-Get-Pattern.aspx, which clearly shows how to implement the PRG design pattern. I have come across this before but never have applied it within the context of ASP.NET MVC 2. After walking through this example myself, I can see where I need to make my adjustments so this definitely shows the direction in which I need to go. Thanks for you prompt reply and input!
Michael
Thanks AaronSieb, as I was returning a View("PageName", model) instance. Just as an additional FYI, my design initially was to have two methods named "Review," which one being a GET and another a POST. In the POST "Review" method, I was attempting to capture which submit button was pressed (e.g., "Next" or "Previous") and return a new ActionResult from one of two corresponding helper methods named "Next" and "Previous." Each of these helper methods would return the content based upon the direction taken by the user (e.g., forward or backward in the wizard). Thanks!
Michael