1058

In ASP.NET MVC, what is the difference between:

  • Html.Partial and Html.RenderPartial
  • Html.Action and Html.RenderAction
abatishchev
  • 92,232
  • 78
  • 284
  • 421
Ghooti Farangi
  • 19,228
  • 14
  • 42
  • 61

13 Answers13

1258

Html.Partial returns a String. Html.RenderPartial calls Write internally and returns void.

The basic usage is:

// Razor syntax
@Html.Partial("ViewName")
@{ Html.RenderPartial("ViewName");  }

// WebView syntax
<%: Html.Partial("ViewName") %>
<% Html.RenderPartial("ViewName"); %>

In the snippet above, both calls will yield the same result.

While one can store the output of Html.Partial in a variable or return it from a method, one cannot do this with Html.RenderPartial.

The result will be written to the Response stream during execution/evaluation.

This also applies to Html.Action and Html.RenderAction.

Arsen Khachaturyan
  • 6,472
  • 4
  • 32
  • 36
GvS
  • 50,659
  • 16
  • 96
  • 138
  • 76
    Do you know why you would use one over the other? – Jason Aug 12 '11 at 21:58
  • 159
    performance-wise it's better to use RenderPartial, as answered here: http://stackoverflow.com/questions/2729815/what-is-the-difference-if-any-between-html-partialview-model-and-html-rende – Vlad Iliescu Oct 19 '11 at 14:21
  • 13
    Thanks for the bit about storing result into a variable. This is the reason to use Partial or Action over the Render counterpart. – David Kassa Mar 04 '13 at 15:14
  • 8
    `Html.Partial()` was created to have a more fluent syntax with Razor. As @Vlad said, `Html.RenderPartial()` is more efficient. – Tsahi Asher Apr 10 '14 at 11:25
  • 2
    @Tsahi that explains why it's used in the MVC project template for _LoginPartial. Thanks. – regularmike Jul 07 '14 at 02:55
  • Maybe related? I found a weird issue while using VS2019. If `@Html.Partial("Whatever")` is the only line in the cshtml file, the page takes a very long time to load. Adding a line break (or any content) before or after fixes this, and using the equivalent `RenderPartial` method does not have this issue. – Calculuswhiz May 19 '21 at 15:56
89

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);
}
CodeBon
  • 1,124
  • 8
  • 9
  • 3
    Are you saying that Html.Partial has better performance than Html.RenderPartial? – numaroth Jun 25 '14 at 21:11
  • 16
    Yes and no, Html.Partial is rendered inline and is less resource intensive but more time consuming. Html.RenderPartial is rendered separately and therefore faster, but is more resource intensive. If you have high volume burst traffic favour Html.Partial to reduce resource usage. If you have infrequent changes in traffic volume favour Html.RenderPartial. – CodeBon Dec 31 '14 at 00:46
  • 4
    In my opinion, it's the other way round: RenderPartial definitely has better performance as it writes directly to the output stream. Partial internally calls the same method, but writes into a StringWriter which is returned as a MvcHtmlString and finally written to the output stream. Therefore it allocates a lot more memory. – slfan Mar 01 '17 at 13:09
  • 2
    @BrettJones What do you mean by "resource intensive"? Just because `Partial` renders into a buffer does not mean it's rendered asynchronously - quite the opposite - I cannot see how you can claim `RenderPartial` is "more resource intensive". – Dai Jul 17 '17 at 09:54
68

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.

David
  • 1,490
  • 1
  • 15
  • 28
  • 2
    Answering (Why?) is the best answer so this is best for me. – YEH Nov 27 '17 at 09:05
  • @David would be kind enough to also elaborate on what to use if [OutputCache] is being employed? I have a gut feeling that if caching is involved then Action / RenderAction are the way to go because they do respect [OutputCache] (in contrast to Partial / RenderPartial which ignore it completely thus hurting performance). I might be wrong though. – XDS Feb 15 '21 at 12:10
55

Difference is first one returns an MvcHtmlString but second (Render..) outputs straight to the response.

Aliostad
  • 76,981
  • 19
  • 152
  • 203
  • wouldn't MvcHtmlString be added to the response as well? – Shad Sep 27 '19 at 07:21
  • Shad, Html.Partial() methods returns the MvcHTMLString, which will be used by razor view engine to add the content to response body. – Johnny Jul 25 '20 at 18:42
22

@Html.Partial and @Html.RenderPartial are used when your Partial view model is correspondence of parent model, we don't need to create any action method to call this.

@Html.Action and @Html.RenderAction are used when your partial view model are independent from parent model, basically it is used when you want to display any widget type content on page. You must create an action method which returns a partial view result while calling the method from view.

shyam
  • 8,153
  • 4
  • 23
  • 40
Jayesh Patel
  • 271
  • 3
  • 6
22

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()

iChirag
  • 1,548
  • 17
  • 39
15

More about the question:

"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."

“NerdDinner” from Professional ASP.NET MVC 1.0

Owen Pauling
  • 9,864
  • 18
  • 50
  • 58
Ygor Thomaz
  • 497
  • 5
  • 14
10

The return type of Html.RenderAction is void that means it directly renders the responses in View where the return type of Html.Action is MvcHtmlString You can catch its render view in controller and modify it by using following method

protected string RenderPartialViewToString(string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
            viewName = ControllerContext.RouteData.GetRequiredString("action");

        ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            return sw.GetStringBuilder().ToString();
        }
    }

This will return the Html string of the View.

This is also applicable to Html.Partial and Html.RenderPartial

Rajesh
  • 1,431
  • 10
  • 17
Shivkumar
  • 1,893
  • 5
  • 19
  • 32
10

Partial or RenderPartial: No need to create action method. use when data to be display on the partial view is already present in model of current page.

Action or RenderAction: Requires child action method. use when data to display on the view has independent model.

Anil
  • 1,065
  • 12
  • 29
9

Differences:

  1. The return type of RenderPartial is void, where as Partial returns MvcHtmlString

  2. Syntax for invoking Partial() and RenderPartial() methods in Razor views

    @Html.Partial("PartialViewName")
    @{ Html.RenderPartial("PartialViewName"); }

  3. 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().

Nazmul Hasan
  • 8,494
  • 5
  • 44
  • 64
5

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

user247702
  • 21,902
  • 13
  • 103
  • 146
Navneet
  • 441
  • 4
  • 13
4

@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();
 }
RickL
  • 2,940
  • 10
  • 31
  • 35
Mayank
  • 151
  • 1
  • 4
3

For "partial" I always use it as follows:

If there's something you need to include in a page that you need to go via the controller (like you would with an Ajax call) then use "Html.RenderPartial".

If you have a 'static' include that isn't linked to a controller per-se and just in the 'shared' folder for example, use "HTML.partial"

scgough
  • 4,672
  • 3
  • 25
  • 40