tags:

views:

43

answers:

2

hi,

what is the life of a controller? if i create a controller, store some data in private fields, then the user goes to other pages using different controllers, then clicks a link to come back to the original controller, under which circumstances will that data still be there?

thanks!

+2  A: 

The controller is instantiated and destroyed by the MVC framework for each HTTP request.

If you need to persist data between page requests, use Session, or store it in a database.

womp
Or Cookies Or TempData :-)
Charlino
Nooooo!!!! a Gipsy's dies every time you use TempData!!! avoid it...If you are storing data between requests using it, you are breaking the stateless nature of ASP MVC : )
SDReyes
I prefer TempData for simple scenarios like a success message after directing to another page. It keeps the url clean, they can't refresh and get the message again like you would get if you put it in the querystring. I think it's ok in light moderation. It wraps the code I would have written against Session myself to do the same thing.
Jab
@Jab - Agreed. I use it for that exact purpose - flash messages.
Charlino
+3  A: 

This is a great cheat sheet for the asp.net mvc lifecycle. Click the image for the original PDF.

alt text

Martijn Laarman