97

What is the appropriate way of rendering a child template?

And what's the difference? Both seem to work for me.

And why does @Html.RenderPartial() no longer work?

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
Evgenyt
  • 10,201
  • 12
  • 40
  • 44

6 Answers6

132
Html.Partial("MyView")

Renders the "MyView" view to an MvcHtmlString. It follows the standard rules for view lookup (i.e. check current directory, then check the Shared directory).

Html.RenderPartial("MyView")

Does the same as Html.Partial(), except that it writes its output directly to the response stream. This is more efficient, because the view content is not buffered in memory. However, because the method does not return any output, @Html.RenderPartial("MyView") won't work. You have to wrap the call in a code block instead: @{Html.RenderPartial("MyView");}.

RenderPage("MyView.cshtml")

Renders the specified view (identified by path and file name rather than by view name) directly to the response stream, like Html.RenderPartial(). You can supply any model you like to the view by including it as a second parameter

RenderPage("MyView.cshtml", MyModel)
Annabelle
  • 10,596
  • 5
  • 27
  • 26
  • 9
    Are there any metrics on the efficiency gain of @{Html.RenderPartial("MyView");} over @Html.RenderPartial("MyView")? – Faust Oct 18 '11 at 07:03
  • @Faust did you meant Partial vd RenderPartial ? – Cacho Santa Aug 12 '14 at 19:24
  • 1
    @cacho: yes, my comment should read `@Html.Partial("MyView")` vs. `@{Html.RenderPartial("MyView");}` – Faust Aug 12 '14 at 19:35
  • 2
    Small correction: the second parameter in RenderPage() isn't the model, it's a param[] Object which is accessed via the PageData property. It appears to work as above because by default the model from the "parent" page is passed through to the "child". https://msdn.microsoft.com/en-us/library/system.web.webpages.webpagebase.renderpage(v=vs.111).aspx – Jon Nov 22 '16 at 16:15
  • Is there a way to nest a complete page along its model by calling `RenderPage`? I want to nest a different page by specifying a specific query parameter, which in turn would filter the data in that page, and in addition, remove its own layout. – Shimmy Weitzhandler Sep 27 '19 at 03:18
17

I prefer

@RenderPage("_LayoutHeader.cshtml")

Over

@{ Html.RenderPartial("_LayoutHeader"); }

Only because the syntax is easier and it is more readable. Other than that there doesn't seem to be any differences functionality wise.

EDIT: One advantage of RenderPartial is you don't have to specify the entire path or file extension it will search the common places automatically.

Ryan Sampson
  • 6,717
  • 12
  • 47
  • 55
6

The RenderPartial method doesn’t return HTML markup like most other helper methods. Instead, it writes content directly to the response stream, which is why we must call it like a complete line of C#, using a semicolon.

This is slightly more efficient than buffering the rendered HTML from the partial view, since it will be written to the response stream anyway. If you prefer a more consistent syntax, you can use the Html.Partial method, which does exactly the same as the RenderPartial method, but returns an HTML fragment and can be used as @Html.Partial("Product", p).

Omid Shariati
  • 1,904
  • 5
  • 22
  • 44
2

We can also pass model using partial views. @Html.Partial("MyView","MyModel");

bayram
  • 64
  • 2
0
@RenderPages() 

The above does not work in ASP.NET MVC. It only works in WebPages.

@Html.Partial("_Footer")

You will need to use the above in ASP.NET MVC.

Umar Aftab
  • 527
  • 4
  • 24
0

For ASP.NET Core 7. In the Shared folder make partial file then user this following code

<partial name="_NavBar" />