1
  • The scenario is

I have a sql select statement that is sent from view to another in a TempData and in the next view i retrieve the sql select statement from the TempData and save it into a ViewBag .. and then use it as usual

  • The Problem is

When i refresh the page everything is gone .. the TempData is gone and of course the ViewBag is empty

Is there any solution to preserve this sql select statement as long as the view is opened in browser .. and then destroyed automatically afterwords after browsing out of the view

Thanks in advance

Mariam
  • 533
  • 2
  • 12
  • 22

1 Answers1

0

If you want a similar option that persists for a longer period, I'll suggest using Session. Below are a few differences between TempData and Session which i think you might find useful.

Session

  1. Session is also used to pass data within the ASP.NET MVC application and Unlike TempData, it persists for its expiration time (by default session expiration time is 20 minutes but it can be increased).
  2. Session is valid for all requests, not for a single redirect.
  3. It’s also required typecasting for getting data and check for null values to avoid error.

TempData

  1. TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).
  2. It’s life is very short and lies only till the target view is fully loaded.
  3. It’s required typecasting for getting data and check for null values to avoid error.

Copied from this website

jamiedanq
  • 967
  • 7
  • 12