Other than the type it returns and the fact that you call it differently of course
<% Html.RenderPartial(...); %> <%= Html.Partial(...) %>
If they are different, why would you call one rather than the other one?
The definitions:
// Type: System.Web.Mvc.Html.RenderPartialExtensions
// Assembly: System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:Program Files (x86)Microsoft ASP.NETASP.NET MVC 2AssembliesSystem.Web.Mvc.dll
using System.Web.Mvc;
namespace System.Web.Mvc.Html
{
public static class RenderPartialExtensions
{
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName);
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, ViewDataDictionary viewData);
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model);
public static void RenderPartial(this HtmlHelper htmlHelper, string partialViewName, object model,
ViewDataDictionary viewData);
}
}
// Type: System.Web.Mvc.Html.PartialExtensions
// Assembly: System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// Assembly location: C:Program Files (x86)Microsoft ASP.NETASP.NET MVC 2AssembliesSystem.Web.Mvc.dll
using System.Web.Mvc;
namespace System.Web.Mvc.Html
{
public static class PartialExtensions
{
public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName);
public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName,
ViewDataDictionary viewData);
public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model);
public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model,
ViewDataDictionary viewData);
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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:
Partialreturns a HTML-encoded stringRenderPartialis 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 usePartial; 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
RenderPartialor
Partialdoesn’t change the final effect. However, because
RenderPartialis slightly faster, you may prefer using it.
Method 3
The Html.Partial and Html.RenderPartial are extension method is a reusable portion of a web page. The return type of RenderPartial is void, where as Partial returns MvcHtmlString. Html.RenderAction will render the result directly to the Response Stream (More efficient & fast) whereas Html.Action returns a string (which can be manipulated) with the result.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0