Home > Software Development > MVC Html.RenderAction vs. Html.RenderPartial?

MVC Html.RenderAction vs. Html.RenderPartial?

I try to avoid “X vs. Y” or “this vs. that” ideological debates.  I tend to think that the context of the current problem should dictate which forest you need to be in.

I think that my tendency is to favor RenderAction over RenderPartial.  This may be due to my style, my bias toward quality, and test first development.  For those of you that know me personally, I am a strong advocate of test first development and I try to follow “The Single Responsibility Principal”

The fact that RenderAction allows you to call into an action method defined in a another controller not related to the controller of the parent view, allows the implementation of the partial view (or whatever is to be rendered) to be decoupled from its parent.

As you can see in the source code below, the RenderAction simply delegates to a controller that only deals with the partial view (not the parent).  The RenderPartial method loads the partial view and injects data into the partial view. 

Based on this observation, RenderAction promotes separation of concerns and loose coupling, and consequently, views that manifest themselves via RenderAction are inherently more testable.

That’s not to say that I would never use RenderPartial.  If the markup in my main view was getting excessively large and the controller of the main view wasn’t already violating the single responsibility principal, and the parent view already had all of the data needed by the partial view, I would probably consider using RenderPartial over RenderAction.

If I had to build a quick down and dirty prototype, I may just consider RenderPartial.  For me a prototype is throwaway code and is rarely tested and should never make it into production, so for this type of scenario I don’t mind skipping test first development.

Another benefit of a RenderPartial would be to test out some UI enhancements before the engineering team was truly committed to delivering the full blown production ready first class feature.

Some practical scenarios:

  • my main menu would probably be loaded via RenderAction
  • contact billing address, mailing address, general info, etc. would probably be loaded via RenderPartial
  • sub menus / secondary navigation OR contextual navigation would probably be via RenderAction
  • specialized web parts or mashup functions from external sources would probably be loaded via RenderAction
  • simple master detail pages would probably be RenderPartial until they got overloaded with features
  • simple views that allows uses to switch between read / edit / and create new would probably be done via RenderPartial

On page 198 of Dino Esposito’s MVC2 book he makes great arguments for RenderAction vs. RenderPartial.  Basically he says that if the parent view already has all of the data, then consider RenderPartial because you already have the data and you wont end up doing all of the Context transfer operations and you can save some performance.  However, if you need to go get the data or return something “other” than an .ascx view or possibly external data, then consider RenderAction.

Performance Note –> From what I understand, RenderAction issues an additional request, and RenderPartial simply injects the content into the existing request pipeline.  So this could be a performance consideration.

Here is a quick snipped that outlines the syntactic difference in two approaches.  Ultimately, the output on the screen is the same.

<h2>View Rendered via RenderPartial</h2>
<% Html.RenderPartial("CloudUserControl", new CloudUserControlViewModel
        (this.ViewData["CloudTags"] as DisplayTags)); %>

<h2>View Rendered via RenderAction</h2>
<% Html.RenderAction<CloudUserControlController>(it => it.Index()); %>

This snippet outlines the C# code in the controller that handles the Html.RenderAction markup above…

public class CloudUserControlController : Controller
    public ActionResult Index()
        var viewModel = new CloudUserControlViewModel();
        //TODO: go fetch some data
        return PartialView("CloudUserControl", viewModel);

The images below show the sample output of the code above…


In any case, that’s my opinion… once again it depends, but I prefer the testability and single responsibility aspects of RenderPartial.  Fortunately, there are other engineers that have have similar opinions (Derik Whittaker being one of them, thanks for your post).

Categories: Software Development Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: