In ASP.NET MVC, what is the difference between:
Html.Partial
and Html.RenderPartial
Html.Action
and Html.
Difference is first one returns an MvcHtmlString
but second (Render..
) outputs straight to the response.
More about the question:
"When Html.RenderPartial() is called with just the name of the partial view, ASP.NET MVC will pass to the partial view the same Model and ViewData dictionary objects used by the calling view template."
“NerdDinner” from Professional ASP.NET MVC 1.0
For "partial" I always use it as follows:
If there's something you need to include in a page that you need to go via the controller (like you would with an Ajax call) then use "Html.RenderPartial".
If you have a 'static' include that isn't linked to a controller per-se and just in the 'shared' folder for example, use "HTML.partial"
@Html.Partial
and @Html.RenderPartial
are used when your Partial view model is correspondence of parent model, we don't need to create any action method to call this.
@Html.Action
and @Html.RenderAction
are used when your partial view model are independent from parent model, basically it is used when you want to display any widget type content on page. You must create an action method which returns a partial view result while calling the method from view.
Partial or RenderPartial: No need to create action method. use when data to be display on the partial view is already present in model of current page.
Action or RenderAction: Requires child action method. use when data to display on the view has independent model.
Html.Partial
returns a String. Html.RenderPartial
calls Write
internally and returns void
.
The basic usage is:
// Razor syntax
@Html.Partial("ViewName")
@{ Html.RenderPartial("ViewName"); }
// WebView syntax
<%: Html.Partial("ViewName") %>
<% Html.RenderPartial("ViewName"); %>
In the snippet above, both calls will yield the same result.
While one can store the output of Html.Partial
in a variable or return it from a method, one cannot do this with Html.RenderPartial
.
The result will be written to the Response
stream during execution/evaluation.
This also applies to Html.Action
and Html.RenderAction
.