Please enable Javascript to correctly display the contents on Dot Net Tricks!

RenderPartial vs RenderAction vs Partial vs Action in MVC Razor

Posted By : Shailendra Chauhan, 13 Jan 2013
Updated On : 02 Apr 2015
Total Views : 244,869   
Support : MVC4 & MVC3
 

There are different ways for rendering a partial view in MVC Razor. Many developers got confused whether to use RenderPartial or RenderAction or Partial or Action helper methods for rendering a partial view. In this article, I would like to expose the difference among Html.RenderPartial, Html.RenderAction, Html.Partial & Html.Action.

Html.RenderPartial

  1. This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template.

  2. This method returns void.

  3. Simple to use and no need to create any action.

  4. RenderPartial method is useful when the displaying data in the partial view is already in the corresponding view model.For example : In a blog to show comments of an article, we would like to use RenderPartial method since an article information with comments are already populated in the view model.

    @{Html.RenderPartial("_Comments");}
    

  5. This method is faster than Partial method since its result is directly written to the response stream which makes it fast.

Html.RenderAction

  1. This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template.

  2. For this method, we need to create a child action for the rendering the partial view.

  3. RenderAction method is useful when the displaying data in the partial view is independent from corresponding view model.For example : In a blog to show category list on each and every page, we would like to use RenderAction method since the list of category is populated by the different model.

    @{Html.RenderAction("Category","Home");} 
    

  4. This method is the best choice when you want to cache a partial view.

  5. This method is faster than Action method since its result is directly written to the HTTP response stream which makes it fast.

Html.Partial

  1. Renders the partial view as an HTML-encoded string.

  2. This method result can be stored in a variable, since it returns string type value.

  3. Simple to use and no need to create any action.

  4. Like RenderPartial method, Partial method is also useful when the displaying data in the partial view is already in the corresponding view model. For example: In a blog to show comments of an article, you can use Partial method since an article information with comments are already populated in the view model.

    @Html.Partial("_Comments")
    

Html.Action

  1. Renders the partial view as an HtmlString .

  2. For this method, we need to create a child action for the rendering the partial view.

  3. This method result can be stored in a variable, since it returns string type value.

  4. Action method is useful when the displaying data in the partial view is independent from corresponding view model.For example : In a blog to show category list on each and every page, we would like to use Action method since the list of category is populated by the different model.

    @{Html.Action("Category","Home");} 
    

  5. This method is also the best choice when you want to cache a partial view.

What do you think?

I hope you will enjoy the tips and tricks while programming with MVC Razor. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

 
Recommended for you
 
About the Author
Shailendra Chauhan

Shailendra Chauhan is an Entrepreneur, Author, Architect, and Corporate Trainer. He has rewarded as Microsoft MVP for his exceptional contributions in Microsoft Visual Studio and Development Technologies.

With more than 7 years in hand experience Shailendra Chauhan is a polymath in the domains of Microsoft .NET technologies and an array of other technologies including JavaScript, AngularJS, Node.js, Ionic and NoSQL Databases to name but a few.

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers , ASP.NET MVC Interview Questions and Answers , AngularJS Interview Questions and Answers and LINQ Interview Questions and Answers. Furthermore he is a technical reviewer for book on ASP.NET MVC 4 Mobile App Development. Know more...
 
Free Interview Books
 
10 SEP
ASP.NET MVC with AngularJS Development (offline)

SAT,SUN 05:00 PM-06:30 PM IST

More Details
7 SEP
ASP.NET MVC with AngularJS Development (online)

Weekdays (Mon-Fri) 07:30 AM-09:00 AM IST

More Details
3 SEP
ASP.NET MVC with AngularJS Development (online)

Weekend (Sat, Sun) 03:00 PM-05:00 PM IST

More Details
29 AUG
NodeJS Development (online)

MON-FRI 06:00 AM-07:30 AM IST

More Details
27 AUG
PPC Marketing (offline)

Sat, Sun     03:00 PM-05:00 PM IST

20 AUG
NodeJS Development (offline)

Sat, Sun     11:00 AM-12:30 PM IST

16 AUG
NodeJS Development (online)

Mon-Fri     09:00 PM-10:30 PM IST

12 AUG
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     09:30 PM-11:00 PM IST

1 AUG
ASP.NET MVC with AngularJS Development (online)

Mon-Fri     07:30 AM-09:00 AM IST

24 JUL
AngularJS Development (offline)

Sat,Sun     08:00 AM-09:30 AM

24 JUL
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     09:30 AM-11:00 AM

14 JUL
PPC Marketing (offline)

Sat, Sun     12:00 AM-02:00 PM IST

11 MAY
.NET Development (offline)

Mon-Fri     9:00 AM-11:00 AM IST

BROWSE BY CATEGORY
 
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 
+