In ASP.NET MVC, what is the difference between:
Html.Partial
and Html.RenderPartial
Html.Action
and Html.
Html.Partial
: returns MvcHtmlString
and slow
Html.RenderPartial
: directly render/write on output stream and returns void
and it's very fast in comparison to Html.Partial
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);
}
@Html.Partial
returns view in HTML-encoded string and use same view TextWriter
object.
@Html.RenderPartial
this method return void
.
@Html.RenderPartial
is faster than @Html.Partial
The syntax for PartialView
:
[HttpGet]
public ActionResult AnyActionMethod
{
return PartialView();
}
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.
According to me @Html.RenderPartial()
has faster execution than @Html.Partial()
due to Html.RenderPartial gives a quick response to Output.
Because when I use @Html.Partial()
, my website takes more time to load compared to @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()
.