The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.
RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.
RenderPartial(HtmlHelper, String) Renders the specified partial view by using the specified HTML helper. RenderPartial(HtmlHelper, String, Object) Renders the specified partial view, passing it a copy of the current ViewDataDictionary object, but with the Model property set to the specified model.
View can basically contains a complete markup which may contain a master view(or master page) with all the design(s) etc. whereas Partial view is only a portion of page or a small markup which don't have master page. It is basically used as user control in mvc and it can be used at more than one views.
The only difference is that Partial
returns an MvcHtmlString
, and must be called inside <%= %>
, whereas RenderPartial
returnsvoid
and renders directly to the view.
If you look at the source code, you'll see that they both call the same internal method, passing a StringWriter for it to render to.
You would call Partial
if you want to view, save, or manipulate the generated HTML instead of writing it to the page.
This is a great explanation by Dino Esposito:
The difference between the two methods may look small and harmless, but it may bite at you if you don’t know how to handle it. The key difference between the two methods is:
Partial
returns a HTML-encoded stringRenderPartial
is a void method that writes directly to the response output stream.The usage of the two methods is slightly different:
@Html.Partial("_yourPartialView") @{ Html.RenderPartial("_yourPartialView "); }
The choice of which to use depends on your requirements. If you need to further manipulate the string being injected in the response stream, you should use
Partial
; otherwise go withRenderPartial
which is-just because it goes straight to the stream-a little faster thanPartial
.In the end, the use-cases for partial views fall in either of two camps. The first is when you create a view by composing together various independent pieces of markup, as below.
<body> @{ Html.RenderPartial("_Header"); } @Html.Partial("_Sidebar") <div class="container body-content"> @RenderBody() </div> @{ Html.RenderPartial("_Footer"); } </body>
In this case, your decision in choosing between
RenderPartial
orPartial
doesn’t change the final effect. However, becauseRenderPartial
is slightly faster, you may prefer using it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With