views:

64

answers:

1

I am working with ASP.net MVC 2 framework, for multiple sites. We have a base site and then sub sites that inherit from a "Core" site that contains 90% of the functionality that the sub sites will use.

In one of the controllers, I am saving some data, adding a UI message to the tempData and then using Response.Redirect.

The redirect works, but the tempdata is empty after the redirect.

I have tried returning "RedirectToAction" and "RedirectToRoute" with the same routing location and while it populates the TempData, the redirect doesn't happen lol..

So I guess in short, is there a way to get tempdata working when using a standard Response.Redirect?

Thanks in advanced!

A: 

TempData is intended for redirects. But in MVC 2+, reading TempData causes the token to be deleted. So code like this:

if (!string.IsNullOrEmpty(TempData["Foo"].AsString()) { foo =  TempData["Foo"].AsString(); }

... is now broken. But this code:

var bar = TempData["Foo"].AsString();
if (!string.IsNullOrEmpty(bar)) { foo = bar; }

...still works.

Craig Stuntz
I have a "debug window" that I am using to print all view data and temp date elements to the screen. There is nothing at all in the temp data collection besides the view data (which I'm not sure why it's placing all view data in the temp data collection to but it is).This issue is not related to how I am retrieving data from the temp data collection. Thank though.
ETHODE Web Development
That debug window alone is enough to modify `TempData`. I don't like the new design, but it is what it is. I can assure you that `TempData` *does* work for redirects.
Craig Stuntz
@craig: I'm seeing the same than Ethode. Making a redirect on the response in the controller constructor does not carry the TempData
Eduardo Molteni
You're redirecting *in the constructor?!?* **Don't!**
Craig Stuntz
Yes, I know, I changed to `OnActionExecuting`, but that is what Ethode must be experiencing.
Eduardo Molteni