146

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.NET\ASP.NET MVC 2\Assemblies\System.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.NET\ASP.NET MVC 2\Assemblies\System.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);
    }
}

3 Answers 3

172

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.

6
  • Ok, so there isn't any :) as I thought, I can use them seamlessly I guess. Thanks :)
    – Stéphane
    Commented Apr 28, 2010 at 13:37
  • 42
    no, the difference is the return type, as SLaks said. This is non-trivial. Performance-wise, it's been claimed that rendering directly to the output stream is better (which was why they went w/ the void RenderPartial to start with). Partial is mostly useful (imo) in testing, though as SLaks said there may be some places that you want to manipulate the output before rendering it in production code. They're just rare, imo.
    – Paul
    Commented Jun 1, 2010 at 19:50
  • 42
    Partial is also helpful when you want to get the output of a view in the controller. This can be used to use a view to format an email.
    – Samg
    Commented Sep 29, 2010 at 17:08
  • 3
    @Paul: How much more performant are we talking? Do the claims have any testing or benchmarks associated with them? It would be interesting to see if there's actual meat to this or if it's just a micro-optimization. Commented Nov 5, 2014 at 20:00
  • 1
    A question I have is: both would end up being rendered to the result stream regardless of which you use (since in Razor isn't just calling it sending it to the rendered view's TextWriter), if they both call the same internal method, why would one be more performant than the other? Is it due to it spinning-up its own internal buffer (which would progressively expand as needed)? Also I'm glad someone else saw use to use the view model to render e-mails; another developer thought I was over-doing it, but it's cleaner and easier to manipulate than a string you have to escape everything in. Commented Jun 18, 2015 at 20:47
5

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 string
  • RenderPartial 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 with RenderPartial which is-just because it goes straight to the stream-a little faster than Partial.

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 or Partial doesn’t change the final effect. However, because RenderPartial is slightly faster, you may prefer using it.

2
  • 4
    -1 When copying from another source, at least give credit. This is a blatant word-for-word copy from Pro ASP.NET MVC 3 Framework (or at least a newer edition).
    – Robotronx
    Commented Apr 2, 2017 at 10:02
  • @Robotronx ooops, I am sorry. Thank for your comment. Please, see my updated reply
    – StepUp
    Commented Aug 3, 2022 at 15:31
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.