Like Html.RenderAction() but without reinstantiating the controller object

Posted by Jørn Schou-Rode on Stack Overflow See other posts from Stack Overflow or by Jørn Schou-Rode
Published on 2009-12-13T13:21:23Z Indexed on 2010/03/09 0:06 UTC
Read the original article Hit count: 364

Filed under:
|

I like to use the RenderAction extension method on the HtmlHelper object to render sidebars and the like in pages, as it allows me to keep the data access code for each such part in separate methods on the controller. Using an abstract controller base, I can define a default "sidebar strategy", which can then be refined by overriding the method in a concrete controller, when needed.

The only "problem" I have with this approach, is that the RenderAction is built in a way where it always creates a news instance of the controller class, even when rendering actions from the controller already in action. Some of my controllers does some data lookup in their Initialize method, and using the RenderAction method in the view causes this to occur several times in the same request.

Is there some alternative to RenderAction which will reuse the controller object if the action method to be invoked is on the same controller class as the "parent" action?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about renderaction