In ASP.NET MVC, what is the difference between:
Html.Partial
andHtml.RenderPartial
Html.Action
andHtml.RenderAction
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
.
Think of @Html.Partial as HTML code copied into the parent page. Think of @Html.RenderPartial as an .ascx user control incorporated into the parent page. An .ascx user control has far more overhead.
'@Html.Partial' returns a html encoded string that gets constructed inline with the parent. It accesses the parent's model.
'@Html.RenderPartial' returns the equivalent of a .ascx user control. It gets its own copy of the page's ViewDataDictionary and changes made to the RenderPartial's ViewData do not effect the parent's ViewData.
Using reflection we find:
public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model, ViewDataDictionary viewData)
{
MvcHtmlString mvcHtmlString;
using (StringWriter stringWriter = new StringWriter(CultureInfo.CurrentCulture))
{
htmlHelper.RenderPartialInternal(partialViewName, viewData, model, stringWriter, ViewEngines.Engines);
mvcHtmlString = MvcHtmlString.Create(stringWriter.ToString());
}
return mvcHtmlString;
}
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName)
{
htmlHelper.RenderPartialInternal(partialViewName, htmlHelper.ViewData, null, htmlHelper.ViewContext.Writer, ViewEngines.Engines);
}
Here is what I have found:
Use RenderAction when you do not have a model to send to the view and have a lot of html to bring back that doesn't need to be stored in a variable.
Use Action when you do not have a model to send to the view and have a little bit of text to bring back that needs to be stored in a variable.
Use RenderPartial when you have a model to send to the view and there will be a lot of html that doesn't need to be stored in a variable.
Use Partial when you have a model to send to the view and there will be a little bit of text that needs to be stored in a variable.
RenderAction and RenderPartial are faster.
@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.
The return type of Html.RenderAction
is void
that means it directly renders the responses in View where the return type of Html.Action
is MvcHtmlString
You can catch its render view in controller and modify it by using following method
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
This will return the Html string of the View.
This is also applicable to Html.Partial
and Html.RenderPartial
Differences:
The return type of RenderPartial
is void
, where as Partial
returns MvcHtmlString
Syntax for invoking Partial()
and RenderPartial()
methods in Razor views
@Html.Partial("PartialViewName")
@{ Html.RenderPartial("PartialViewName"); }
Syntax for invoking Partial()
and RenderPartial()
methods in webform views
[%: Html.Partial("PartialViewName") %]
[% Html.RenderPartial("PartialViewName"); %]
The following are the 2 common interview questions related to Partial()
and RenderPartial()
When would you use Partial()
over RenderPartial()
and vice versa?
The main difference is that RenderPartial()
returns void and the output will be written directly to the output stream, where as the Partial()
method returns MvcHtmlString
, which can be assigned to a variable and manipulate it if required. So, when there is a need to assign the output to a variable for manipulating it, then use Partial(), else use RenderPartial().
Which one is better for performance?
From a performance perspective, rendering directly to the output stream is better. RenderPartial()
does exactly the same thing and is better for performance over Partial()
.
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"