In this post, we are going to discuss aboutย Html.RenderPartial vs Html.Partial, Html.RenderAction vs Html.Action.ย Let we split this stuff in two ways ย based on binding the static and dynamic content like Render partial and Action Partial .
Partialย Vs RenderPartial
- @Html.Partial(“Details”) ย –ย ย returns a String as an output
- @{ Html.RenderPartial(“Details”); }ย – ย returns void as an output
Action Vs RenderAction
- @Html.Action(“ActionName”, “ControllerName”) –ย returns a String as an output
- @{ Html.RenderAction(“ActionName”, “ControllerName”); }ย – ย returns void as an output
Render vs Action partial
Let us discuss about the mainย difference between RenderActionย and RenderPartial.ย RenderPartialย will render the viewย on the same controller. But RenderActionย will execute the action method , thenย builds a model and returns a view result.
Render Partialย : Use Html.Partial when you are rendering static content.
Action Partial : Use Html.Action when handling dynamic Content/data.
Where to use Html.Partial and Html.Action
Html.Partial :ย If we need to display a static menu for all users from the partial view, then we can useย Render partial.
Example:
View :ย Here we call the partial view from the view (passing model as input).
@Html.Partial(“_partialName”, model)
Html.Action:ย If we need to display different menu items based on the user level, then we can use Action Partial.
Example:
Controller:
public ActionResult CustomerDetail() {
return PartialView(new CustomerDetailModel());
}
View
@model Model
@Html.Action(“CustomerDetail”)
Difference :
1) @Html.RenderPartial and @Html.RenderAction is the best practice to use in most cases because the output has been written in the same TextWriter object.
2) In contrast to this, @Html.Partial and @Html.Action methods create their own TextWriter instances at every time and buffer all their content into memory.
3) @Html.Actionย and @Html.RenderActionย method is the best choice when you want to cache a partial view.
@Html.Partial and Html.RenderPartial method is the best choice when you want to load new view.
4) The return type forย @Html.Partial andย Html.Actionย returns string type values
@Html.RenderAction andย Html.RenderPartial returns is void
5) @Html.Partial and @Html.Action will useful when assign the content into a variable for other purpose…
Have a cool coding………
Thanks for findings Ashish. its updated.
HI,
The return type for @Html.Partial and Html.Action is void
@Html.RenderAction and Html.RenderPartial returns string type values
This is wrong statement. Please modify it because return type for @Html.Partial and Html.Action is string and @Html.RenderAction and Html.RenderPartial returns void
The return type for @Html.Partial is string and Html.Action is MvcHtml String
good