mark smith
mark smith

Reputation: 20897

asp.net MVC: TempData, once read do i need to resave?

i am using TempData to store the referrer for a website and via jquery i am issueing ajax calls to send emails ... I use tempdata to recover the original referrer URL.

It works great on the first read but then the second its empty.... I think this is by design... so i decided to try viewdata but this is stored but when read via the controller on an ajax call it is empty..

Does anyone know what my options are?

Here is the syntax of both lines

        TempData["referrer"] = referrer;  // WORKS great on first read and then is NULL
        ViewData["referrer"] = referrer; // IS STORED but on first read is NULL

Any help really appreciated

Upvotes: 1

Views: 656

Answers (2)

Simon Steele
Simon Steele

Reputation: 11608

TempData persists only for the next request, as you've seen - it's really there for when you're performing something like a redirect and want to be sure that the next call has the data it needs. See this link for good detail:

TempData is really RedirectData

Note that link suggests that you may need to check both ViewData and TempData for a key.

Instead of these two facilities you could consider using Session to store data for the entire user session, or consider stashing your information in the query string or a hidden form field.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Session["referrer"] = referrer;

Upvotes: 0

Related Questions