What is difference between Html.Partial() and Html.RenderPartial() in Asp.net Mvc?

Dung Do Tien Dec 11 2020 501

In Asp.Net MVC, I have a Header partial view. I want to import it to my master page, I used Html.Partial() to import it.

But in Html helper of Asp.net MVC provided another method to help import a partial view is Html.RenderPartial() so what is the difference between them? and what is better?

Html.Partial() and Html.RenderPartial()

Have 2 answer(s) found.
  • M

    Marry Christ Dec 14 2020

    Syntax:

    // Razor syntax ASP.NET MVC
    @Html.Partial("ViewName")
    @{ Html.RenderPartial("ViewName");  }
    
    // Razor syntax ASP.NET Core
    @await Html.PartialAsync("ViewName")
    @{ Html.RenderPartialAsync("ViewName");  } 

    Differences:

    Html.Partial() returns a String.

    Html.RenderPartial() returns void, it writes directly to the response of the application. 

    About performance because RenderPartial() writes directly to the response stream so it's better.

  • M

    Manish Kumar Dec 14 2020

    Html.Partial()

    Returns Html string.

    Injects the Html string of the partial view into the main view.

    Performance is slow.

    No need to be inside the braces.

     

    Html.RenderPartial():

    Returns void.    

    Writes Html in the response stream.

    Perform is faster compared with HtmlPartial().

    Must be inside braces @{ }.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close