Simon Green's Developer Blog
Developing .NET in the cold white north ...

When to use RenderAction vs RenderPartial with ASP.NET MVC

Saturday, 21 February 2009 11:36 by simon

At first glance, RenderAction and RenderPartial both do a very similar thing – they load ‘some other content’ into the view being rendered at the place they are called. Personally, I think they should be used for different scenarios so these are my thoughts on where each one should be used and why.

First though, a quick recap on what they do:

  • RenderPartial renders a control with some model passed to it.
  • RenderAction (or RenderSubAction which addresses some issues) calls a controller action and then renders whatever view that returns with whatever model that controller action passes through it.

Hmmn, they sound pretty similar don’t they! The thing to note though is that the model passed to RenderPartial is either the current model being rendered by the calling view or a subset of it. Anything that a RenderPartial view being called is going to need has to be passed into the Model of the calling view. The view rendered using RenderAction on the other hand could contain a completely different model with no need for this to be passed in to our parent view.

Because of this, I think RenderPartial is most appropriate when what it is going to output could be considered part of the calling view but separating it out into a user control makes sense to allow re-use and avoid repeating the same rendering code in multiple views. For example, if you are rendering a person name in lots of places then instead of repeating within each view the code to output it:

<a href="<%= Url.Action("Display", "Person", new { id = Model.Person.Id}) %>">
  <%= Model.Person.Salutation %> <%= Model.Person.Forename %> <%= Model.Person.Surname %>
</a>


Instead, you move this to a separate user control such as ‘PersonName.ascx’ which expects a Person as the model:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Person>" %>
<a href="<%= Url.Action("Display", "Person", new { id = Model.Id}) %>">
    <%= Model.Salutation %> <%= Model.Forename %> <%= Model.Surname %>
</a>


Now, any place a view wants to output a persons name then you can instead call a PartialView and pass in the appropriate model:

<%= Html.RenderPartial("PersonName", Model.Person); %> 


Why would you want to do this? Well, it helps consistency and avoids repetition which makes it easy if, for example, you decide that name should not longer be in the format “Mr John Smith” but instead “Smith, John (Mr)” and you want to throw in a little jQuery magic to display a profile popup whenever anyone hovers the mouse over the name. This is a simple example but hopefully it demonstrates some benefits of using it compared to repeating the code in each view – with larger chunks of output the benefits of using RenderPartial would be even more apparent

So that is where and how I think RenderPartial should be used. How about RenderAction?

Well, I think this has it’s place when the thing that needs to be rendered isn’t the responsibility of the calling view or controller.

For example, I may have a PersonController responsible for CRUD operations on the Person class including a Display action and view but I do not want either of these to have any responsibility for anything to do with the display of Projects that the person is working on. If I want to display a list of assigned projects within the Person Display view then I would use RenderAction to add it but the responsibility and knowledge of how to do this resides with the ProjectController and it’s views. This would be called as follows:

<% Html.RenderAction("List", "Project", new {personId = Model.Person.Id}); %> 


(incidentally … note that, unlike many of the other extension methods, RenderAction doesn’t return a string so there is no ‘=’ at the beginning and a ‘;’ at the end)

Now, how the Project List is retrieved and rendered is completely the concern of the ProjectController class as it should be and the output can contain whatever it needs to display the list, provide actions to edit project entries and so on.

Another benefit of this approach is if you create different skinned versions of an app. By keeping things modular it is much easier to decide to include something in the view for one skin but not another. So, the skin (set of views) for a full desktop browser may call RenderAction to include the list in the same page whereas the skin for a mobile device friendly interface (think iPhone) would perhaps just include a link instead – both are handled by changing the view instead of changing the controllers which could otherwise be the case.


Comments

June 29. 2009 22:10

SEO

Useful article.
Thanks for helpful information you catch up us with your instructional explanation.

SEO

July 22. 2009 20:15

Thank You Comment

finally i find something that i want to know..
thanks for this usefull informations..

Thank You Comment

August 2. 2009 04:26

cosmetic dentist Northampton

Very interesting post. Might be old, but it was new to me. Good thing you have come up with such blog! Highly recommended! Keep it up!

cosmetic dentist Northampton

August 2. 2009 21:58

enhances

I also saw a lot of advantages to this approach, it seems to me that a lot of people will use this method.

enhances

August 7. 2009 01:30

Alonzo West

Great post, thanks!

Alonzo West

August 18. 2009 05:10

Jerry

It is not wise to mix RenderPartial and RenderAction in a single View because RenderAction will modify the Context of the containing View. For example. Say I have a View made up of several RenderPartial and a single RenderAction. Each RenderPartial uses the ViewData as does the RenderAction. When RenderAction is called the ViewData for the page in which the RenderAction exist has now changed to the ViewData of the RenderAction Controller Action.

Jerry

September 5. 2009 00:04

Beltshumeltz

Thanks that explanation was very helpful.

Beltshumeltz

September 16. 2009 07:00

aion kina

When is the next post comming on this topic.


Regards

Marks

aion kina

September 16. 2009 07:38

aion kina

Now this is hghly recommeded post for me. I will surely email this to my friend.


Regards

Whitr

aion kina

September 20. 2009 20:13

Acne


Admiring the time and effort you put into your blog and detailed information you offer!

Acne

September 29. 2009 08:39

behavior based safety

RenderAction and RenderPartial seems to be the same but each one of them had a certain feature that will explain you how they differ from one another

behavior based safety

October 1. 2009 03:16

Dubai Hotel

Have you ever considered adding more videos to your blog posts to keep the readers more entertained? I mean I just read through the entire article of yours and it was quite good but since I'm more of a visual learner,I found that to be more helpful well let me know how it turns out! I love what you guys are always up too. Such clever work and reporting! Keep up the great works guys I've added you guys to my blogroll. This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.

Dubai Hotel

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading