Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
Asked Answered
A

13

1085

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

  • Html.Partial and Html.RenderPartial
  • Html.Action and Html.RenderAction
Almswoman answered 9/3, 2011 at 15:41 Comment(1)
D
1278

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.

Delightful answered 9/3, 2011 at 15:44 Comment(6)
Do you know why you would use one over the other?Hel
performance-wise it's better to use RenderPartial, as answered here: #2730315Obstruction
Thanks for the bit about storing result into a variable. This is the reason to use Partial or Action over the Render counterpart.Morbidity
Html.Partial() was created to have a more fluent syntax with Razor. As @Vlad said, Html.RenderPartial() is more efficient.Zackzackariah
@Tsahi that explains why it's used in the MVC project template for _LoginPartial. Thanks.Herrington
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.Pyle
J
91

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);
}
Jaclin answered 18/4, 2012 at 14:36 Comment(4)
Are you saying that Html.Partial has better performance than Html.RenderPartial?Sabadell
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.Jaclin
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.Forego
@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".Physiography
L
73

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.

Luteal answered 18/11, 2015 at 20:55 Comment(2)
Answering (Why?) is the best answer so this is best for me.Billibilliard
@Luteal 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.Robot
G
55

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

Glossa answered 9/3, 2011 at 15:44 Comment(2)
wouldn't MvcHtmlString be added to the response as well?Microscopy
Shad, Html.Partial() methods returns the MvcHTMLString, which will be used by razor view engine to add the content to response body.Nucleus
L
25

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

Lambeth answered 23/3, 2015 at 13:10 Comment(1)
Good answer, because you also explain when to use Partial over ActionCheckerbloom
T
23

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

Tetter answered 4/2, 2012 at 4:43 Comment(0)
G
16

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

Gorky answered 2/10, 2012 at 22:5 Comment(0)
C
12

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

Cogen answered 13/5, 2016 at 4:3 Comment(0)
K
11

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.

Keene answered 7/2, 2016 at 12:58 Comment(0)
C
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

Cupping answered 12/9, 2012 at 6:8 Comment(2)
How can I do this with Html.RenderPartial?Falda
we cannot use Html.RenderPartial because its return type is voidPolyneuritis
M
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

Martinemartineau answered 25/8, 2014 at 13:3 Comment(0)
M
5

@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();
 }
Methodist answered 26/9, 2015 at 19:37 Comment(0)
I
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"

Ilocano answered 10/3, 2015 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.