Tuesday, April 5, 2011

add to TempData from non controller class

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

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

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

  • 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.

    Slee : 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.
  • 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).

0 comments:

Post a Comment