Pregunta

Imagine a page with a form input:

Search Criteria: crackers                  


From: ian@stackoverflow.com          
To: list@stackoverflow.com      
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

¿Fue útil?

Solución

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.

Otros consejos

I would either store the necessary data in the Session (I'd only use this for larger amounts of data - and watch out for stale data bugs), or pass values on the querystring in your redirect (this is less prone to bugs and will likely work better for small amounts of data).

It doesn't fit your pattern but Server.Transfer may be useful as well.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top