views:

88

answers:

2

Hi there,

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

A: 
Session["referrer"] = referrer;
Darin Dimitrov
+2  A: 

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.

Simon Steele
This is correct for MVC 1 and MVC 2 prior to RC 1. For MVC 2, RC 1 and later, it has changed and the key is removed after the first read. My wild guess (he doesn't say) is that he's using the latest MVC 2, since that would explain what he sees.
Craig Stuntz
No I am using MVC1 .... With sessions it seems to work but i always though with asp.net mvc it was best to stay away from sessions? Viewdata i never got to work... always on the first read it is NULL
mark smith
There's no particular reason to avoid Session because you're using MVC; indeed, TempData is implemented with Session. That said, with Session the likelihood of stale data is high, since it's retained until you remove it. It's not clear to me why you want to store the referrer (which is available via Request anyway) over multiple hops.
Craig Stuntz