Search code examples
asp.net-mvc-2tempdata

TempData and ViewData die pretty quickly


Is there a way that I can store variables for long periods of time? My users will be leaving their windows open for extended periods of time (Up to a couple (3) of hours). If they do that, then there is a chance that the values will become null. If am correct, ViewData goes null as soon as the view is loaded. What would I do that?


Solution

  • Your best bet would be to use Session State. Or, if your users are authenticating somehow, persist the information relative to the account in a database somewhere.

    If you want to use session, you can use something like this in a controller's action method:

    Session["Key"] = somevalue;
    

    Now, session state dies eventually as well, such as when the user closes their browser, or has been inactive for a while. The default is 20 minutes. If you want to change this, modify the timeout attribute under the sessionState configuration element of your web.config.