tags:

views:

106

answers:

3

How do I add to the TempData dictionary from a non controller class?

A: 

To do so you'd need the current controller context, otherwise you cannot.

ViewContext.Controller.TempData["whatever"] = whatever

Kyle West
+1  A: 

Just don't do it :D. Everything needs to go through your controller, I know its a pain but that is the right way of doing it.

Al Katawazi
I am using a strongly typed session state class, if a certain value does not exist I wasn't to add a message to the tempdata, so rather than repeat code on every single controller action that access this value I want to add the notification at that point.
Slee
A: 

You would have to pass the TempDataDictionary to the other class. I do this quite a bit and there is nothing wrong with it as long the other class is related to presentation (which it sounds like it is).

liammclennan