ChatGPT解决这个技术问题 Extra ChatGPT

Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

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

Html.Partial and Html.RenderPartial

Html.Action and Html.RenderAction

stackoverflow.com/a/30258091/4336332 this might help as well. take a look.

A
Arsen Khachaturyan

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.


Do you know why you would use one over the other?
performance-wise it's better to use RenderPartial, as answered here: stackoverflow.com/questions/2729815/…
Thanks for the bit about storing result into a variable. This is the reason to use Partial or Action over the Render counterpart.
Html.Partial() was created to have a more fluent syntax with Razor. As @Vlad said, Html.RenderPartial() is more efficient.
@Tsahi that explains why it's used in the MVC project template for _LoginPartial. Thanks.
C
CodeBon

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);
}

Are you saying that Html.Partial has better performance than Html.RenderPartial?
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.
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.
@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".
D
David

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.


Answering (Why?) is the best answer so this is best for me.
@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.
A
Aliostad

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


wouldn't MvcHtmlString be added to the response as well?
Shad, Html.Partial() methods returns the MvcHTMLString, which will be used by razor view engine to add the content to response body.
s
shyam

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


Good answer, because you also explain when to use Partial over Action
R
Risto Välimäki

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


O
Owen Pauling

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


A
Anil

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.


N
Nazmul Hasan

Differences:

The return type of RenderPartial is void, where as Partial returns MvcHtmlString Syntax for invoking Partial() and RenderPartial() methods in Razor views @Html.Partial("PartialViewName") @{ Html.RenderPartial("PartialViewName"); } 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().


R
Rajesh

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


How can I do this with Html.RenderPartial?
we cannot use Html.RenderPartial because its return type is void
u
user247702

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


R
RickL

@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();
 }

s
scgough

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"