RenderPartial vs RenderAction vs Partial vs Action in MVC Razor

RenderPartial vs RenderAction vs Partial vs Action in MVC Razor

04 Apr 2024
Intermediate
381K Views
5 min read

Partial View: An Overview

There are different ways of rendering a partial view in MVC Razor. Many developers got confused about whether to use RenderPartial RenderAction or Partial or Action helper methods for rendering a partial view. In this MVC Tutorial, we will explore more about the difference between RenderPartial, RenderAction, Partial, and Action which will include RenderPartial vs RenderAction vs Partial vs Action in MVC Razor. Consider our ASP.NET MVC Course for a better understanding of all MVC core concepts.

What is Partial view:

  • A partial view is nothing but a Razor markup file
  • Its extension is .cshtml.
  • It renders HTML output within another markup file.
  • It is not a complete view.
  • A partial view is used for the reusability of the HTML markup.

Different ways of rendering partial view:

There are different ways of rendering partial view in MVC Razor. These are as follows:

  • RenderPartial
  • RenderAction
  • Partial
  • Action

When to use:

  • It is used to break up large markup files into smaller components.
  • It is used to reduce the duplication of common markup content across markup files.
  • It shouldn't be used to maintain common layout elements.

Html.RenderPartial

  1. This method result will be directly written to the HTTP response stream means it uses 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. The 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 on an article, we would like to use the RenderPartial method since article information with comments is already populated in the view model.

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

  5. This method is faster than the 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 uses the same TextWriter object as used in the current webpage/template.

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

  3. The renderAction method is useful when the displaying data in the partial view is independent of the corresponding view model.For example: In a blog to show a 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 the 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 a string type value.

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

  4. Like the RenderPartial method, the 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 on an article, you can use the Partial method since article information with comments is 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 rendering the partial view.

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

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

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

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

RenderPartial vs Partial

Partial

It returns a MvcHtmlString object. It stringifies the HTML content in the location where it was specified.

RenderPartial

This method will not actually return any values or strings and instead, it will write the Partial View that is requested to the Response Stream through response. So write internally.

RenderAction vs Action

RenderAction

This method renders the result directly to the response which means it is more efficient if the action returns a large amount of HTML.

Action

It returns a string with the result.

Conclusion:

So in this article, we have learned about RenderPartial vs RenderAction vs Partial vs Action in MVC Razor. I hope you enjoyed learning these concepts while programming with Asp.Net. Feel free to ask any questions from your side. Your valuable feedback or comments about this article are always welcome. Level up your career in MVC with our ASP.Net Core Certification.

FAQs

Q1. When to use partial and RenderPartial in MVC?

It is useful when the displaying data in the partial view is already in the corresponding view model.

Q2. What is the difference between action and render action?

Html. RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html. Action returns a string with the result.

Q3. What is render action in MVC?

It will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas HTML. Action returns a string with the result.
Share Article
Batches Schedule
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this