After a pair programming session, an interesting question came up which I think I know the answer for.
Question: Is there any other desired way in ASP.NET MVC to retain ‘state’ other than writing to database or a text file?
I’m going to define state here to mean that we have a collection of person objects, we create a new one, and go to another page, and expect to see the newly created person. (so no Ajax)
My thoughts are we don’t want any kung-fu ViewState or other mechanisms, this framework is about going back to a stateless web.
The example you provided is pretty easy to do without any sort of “view state kung fu” using capabilities that are already in MVC. “User adds a person and sees that on the next screen.” Let me code up a simple
PersonControllerthat does exactly what you want:Now, what I’m sensing is that you want person on yet another page after
PersonSaveSuccessor something else. In that case, you probably want to useTempData[""]which is a single serving session and only saves state from one request to another or manage the traditionalSession[""]yourself somehow.What is confusing to me is you’re probably going to the db to get all your persons anyway. If you save a person it should be in your persons collection in the next call to your
GetPersons(). If you’re not using Ajax, than what state are you trying to persist?