Razor: @Html.Partial() vs @RenderPage()

asp.net MvcRazorRenderpartial

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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)

Solution 2 - asp.net Mvc

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.

Solution 3 - asp.net Mvc

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

Solution 4 - asp.net Mvc

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

Solution 5 - asp.net Mvc

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEvgenytView Question on Stackoverflow
Solution 1 - asp.net MvcAnnabelleView Answer on Stackoverflow
Solution 2 - asp.net MvcRyan SampsonView Answer on Stackoverflow
Solution 3 - asp.net MvcOmid ShariatiView Answer on Stackoverflow
Solution 4 - asp.net MvcbayramView Answer on Stackoverflow
Solution 5 - asp.net MvcUmar AftabView Answer on Stackoverflow