Search code examples
asp.netpost-redirect-get

How to maintain form state after Post-Redirect-Get in ASP.net?


Imagine a page with a form input:

Search Criteria: crackers                  


From: [email protected]          
To: [email protected]      
Subject: How to maintain form state with PRG?
Message: Imagine a page with form input:              
 
 
 
 
 

Send

After the user clicks Send, the server will instruct to client to Redirect, as part of the Post-Redirect-Get pattern.

POST /mail/u/compose

HTTP/1.1 303 See Other
Location: https://stackoverflow.com/mail/u/compose

And the client will issue a GET of the new page. The problem is that some elements of the existing form are lost:

Search Criteria:                   

It gets worse when there are a few drop-downs, and checkboxes.

How can i maintain form state in using Post-Redirect-Get in ASP.net, given that the viewstate is then non-existent.


Bonus Reading


Solution

  • If the search criteria value is supposed to persist across many different pages in the system (in other words, throughout the user's session), it sounds like a candidate to be stored in the Session. Or, if it is supposed to persist across multiple sessions (ie, next time the user logs in, it remembers the search criteria he used last time), it will need to be persisted somewhere more permanent.

    I'm not aware of anything in ASP.NET which behaves like MVC's "temp" space, where the value is automatically cleaned up on first read. Though of course, a helper class (or convenience function) which wraps the Session could perform that sort of "read-once" functionality.

    Finally, you could pass the search criteria as a URL parameter when you re-direct. However, that's a little cumbersome (especially if you have multiple fields), and you need to sanitize the incoming data.