This project is read-only.

Pass data to part driver

Topics: Customizing Orchard
Mar 13, 2013 at 10:47 PM

I am currently passing data to a page via return RedirectResult(url with query string), but I would like to avoid this if possible by prepulating the part model or use tempdata.

Is there an existing way to inject controllercontext into a driver so tempdata is available?

Thank you
Mar 14, 2013 at 11:47 AM
Edited Mar 14, 2013 at 11:47 AM
I don't know if there's an existing mechanism to get to the controller context from another class, but there are at least 3 ways I know of to work with it.
  1. Follow the approach INotifier takes: Notifier maintains a list of notification entries. The NotifyFilter, which implements IResultFilter, injects the INotifier and adds the entries to TempData which is available via the context argument in OnActionExecuted.
  2. Implement an IActionFilter and its OnActionExecuting method, and inject a custom service class, say IControllerContextAccessor, that can store a reference to ControllerContext. Any class that needs access to ControllerContext can inject IControllerContextAccessor and feast on it.
  3. If you created your own controller, you can pass the controller context via arguments, or store it in "global" space such as HttpContext.Current.Items, or using WorkContext.SetState<T> and WorkContext.GetState<T>. This is probably the most horrendous option, but it does get the job done.
Mar 14, 2013 at 7:49 PM
Thank you sky

I pick number 2 but don't know how to inject a service into an action filter.. are there any examples?
Mar 14, 2013 at 8:31 PM
Your action filter is a class that will be inheriting from Orchard.Mvc.Filters.FilterProvider, so I actually meant to say: "inject your service via the constructor". If you're unfamiliar with dependency injection concepts, there are really great courses on pluralsight. In Orchard, I think 95% of the classes use constructor injection, so loads of examples to be found there.
Mar 14, 2013 at 9:49 PM
Thanks again, just the nudge I needed - I didn't realize that IActionFilter decorated all actions - perfect.