21
NovRenderPartial vs RenderAction vs Partial vs Action in MVC Razor
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.
Read More: Top 50 MVC Interview Questions and Answers |
Html.RenderPartial
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.
This method returns void.
Simple to use and no need to create any action.
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");}
This method is faster than the Partial method since its result is directly written to the response stream which makes it fast.
Html.RenderAction
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.
For this method, we need to create a child action for rendering the partial view.
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");}
This method is the best choice when you want to cache a partial view.
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
Renders the partial view as an HTML-encoded string.
This method result can be stored in a variable since it returns a string type value.
Simple to use and no need to create any action.
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
Renders the partial view as an HtmlString.
For this method, we need to create a child action for rendering the partial view.
This method result can be stored in a variable since it returns a string type value.
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");}
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.