Thursday, 26 December 2013

RenderPartial vs RenderAction vs Partial vs Action in MVC Razor

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 used 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.
  5. This method is faster than Partial method since its result is directly written to the response stream which makes it fast.
@{Html.RenderPartial("_Comments");}

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.
  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.RenderAction("Category","Home");} 

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. Partial method is useful used 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.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.
  5. This method is also the best choice when you want to cache a partial view.
@{Html.Action("Category","Home");} 


0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (49) CD (1) CI (2) CloudComputing (2) Coding (8) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (19) Entity Framework (4) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives