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
- 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.
- This method returns void.
- Simple to use and no need to create any action.
- 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.
- @{Html.RenderPartial("_Comments");}
- This method is faster than 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 used the same TextWriter object as used in the current webpage/template.
- For this method, we need to create a child action for the rendering the partial view.
- 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");}
- This method is the best choice when you want to cache a partial view.
- This method is faster than 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 string type value.
- Simple to use and no need to create any action.
- 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
- Renders the partial view as an HtmlString .
- For this method, we need to create a child action for the rendering the partial view.
- This method result can be stored in a variable, since it returns string type value.
- 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");}
- This method is also the best choice when you want to cache a partial view.
No comments:
Post a Comment