views:

232

answers:

1

Hi, I have an ActionResult that sets TempData to be an object.

The page that is rendered contains a button, that launches a JSON request to the same controller. (For jqGrid population).

The TempData on the JSON Request is null - why does this happen?

Even more bizarrely, if I had a button that posts to the same controller, and I click it instead of my JSON request button, TempData will be there, but if I click the JSON button, then the post button, TempData will be null.

Help appreciated.

Thanks, Chris

+3  A: 

Do not try to pass data from one action to another via TempData when not redirecting. TempData is only for redirects. It is quite likely that some other element of your page is making a request before you press the button, causing the items you've squirreled away in TempData to vanish. That's why TempData is for redirects only; only when redirecting can you have any confidence at all of what the next request will be.

Instead, put the data in the rendered page. When you need to request an action that needs this data, pass it explicitly as a query string parameter.

HTTP is stateless. Learn to live with that. Don't try to introduce state to your server; you'll regret it if you do.

Craig Stuntz