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

What is the appropriate way to play the child template?

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

And why does @Html.RenderPartial() not work anymore?

+85
asp.net-mvc razor renderpartial
Dec 21 2018-10-21
source share
4 answers
 Html.Partial("MyView") 

Displays the "MyView" MvcHtmlString on an MvcHtmlString . It follows the standard rules for viewing the view (for example, check the current directory, then check the Shared directory).

 Html.RenderPartial("MyView") 

Same as Html.Partial() , except that it writes its output directly to the response stream. This is more efficient because the contents of the view are not buffered in memory. However, since the method does not return any output, @Html.RenderPartial("MyView") will not work. Instead, you should transfer the call to a block of code: @{Html.RenderPartial("MyView");} .

 RenderPage("MyView.cshtml") 

Maps the specified view (identified by the path and file name, not the name of the view) directly to the response stream, for example, Html.RenderPartial() . You can provide any model you like for presentation by including it as a second parameter

 RenderPage("MyView.cshtml", MyModel) 
+115
Jun 15 '11 at 17:25
source share

I prefer

 @RenderPage("_LayoutHeader.cshtml") 

More

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

Just because the syntax is simpler and more readable. Other than that, there seems to be no difference in functionality.

EDIT: One advantage of RenderPartial is that you don’t need to specify the entire path or file extension, and it will automatically search for ordinary places.

+15
Mar 18 2018-11-18T00:
source share

The RenderPartial method does not return HTML markup, like most other helper methods. Instead, it writes the content directly to the response stream, so we should call it as a complete C # line using a semicolon.

This is slightly more efficient than buffering the displayed HTML from a partial view, as 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 the same as the RenderPartial method, but returns an HTML fragment and can be used as @ Html.Partial ("Product", p).

+6
Jan 26 '13 at 6:53
source share

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

+2
Aug 12 '14 at
source share



All Articles