views:

306

answers:

4

Hi Guys

I'm a little bit unsure about when it's appropriate to use Html.RenderAction() to render my Views and when not to. My understanding is that because it's not an 'official' component of ASP.NET MVC it's bad practice to use it, and its original intention was for reusable widgets that don't exist in any specific Controller context.

The thing is, RenderAction is very useful when I need a component that exists under a different Controller than the one that I'm currently rendering the View for. I think it's a very tidy & self contained way to render components that rely on data unavailable in the current View. My View doesn't need to supply the Model, as it would if I was using RenderPartial()

Is this bad practice? Is there a better way?

+1  A: 

I think it's a very tidy & self contained way to render components that rely on data unavailable in the current View. My View doesn't need to supply the Model, as it would if I was using RenderPartial()

It actually is. For instance, you could create some small view to serve as widgets and inject them wherever you need. Processing user input from those widgets however may get even complicated, but that's another issue.

One other legitimate scenario I can think of is using HTML email templates. It's one case when you clearly don't need to return the rendered output directly to the browser but rather insert it into email body.

Developer Art
+1  A: 

It's OK in case it solves your problem.

Mike Chaliy
+1  A: 

Here's a good blog post that explains some pros and cons. RenderPartial vs RenderAction

gautema
+1  A: 

I use Html.RenderAction() for the reasons you give, so that you don't have to supply the same data over and over again to every single view that needs to display user information (for example). You can argue that it is violating the mvc pattern as the view now knows about the controller. But I think that the advantages in this scenario outweighs that and your application will be more DRY.

I simply use it for everything that I need to reuse in many different places (for example user data that I display on every page from my master page) and I don't want to send that information explicitly to each view. If I'm not mistaken I think that they have included it in asp.net mvc 2 as well, so It's now part of the framework.

Mattias Jakobsson