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().
@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();
}
"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."
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.
Differences:
The return type of
RenderPartial
isvoid
, where asPartial
returnsMvcHtmlString
Syntax for invoking
Partial()
andRenderPartial()
methods in Razor viewsSyntax for invoking
Partial()
andRenderPartial()
methods in webform viewsThe following are the 2 common interview questions related to
Partial()
andRenderPartial()
When would you usePartial()
overRenderPartial()
and vice versa?The main difference is that
RenderPartial()
returns void and the output will be written directly to the output stream, where as thePartial()
method returnsMvcHtmlString
, 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 overPartial()
.@Html.Partial
returns view in HTML-encoded string and use same viewTextWriter
object.@Html.RenderPartial
this method returnvoid
.@Html.RenderPartial
is faster than@Html.Partial
The syntax for
PartialView
:Html.Partial
: returnsMvcHtmlString
and slowHtml.RenderPartial
: directly render/write on output stream and returnsvoid
and it's very fast in comparison toHtml.Partial
More about the question:
“NerdDinner” from Professional ASP.NET MVC 1.0
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:
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.