Pages in ASP. NET MVC tend to have a lot of reuse where they can be packaged for reuse.
Using partial views has the following advantages: 1. You can abbreviate the code. 2. The page code is clearer and better maintained.
There are several ways to load a partial view in a view, including: Partial (), renderpartial (), Action (), Renderaction (), Renderpage () methods
I. Partial and renderpartial
1.Razor syntax: @Html. Partial () and @{html.renderpartial ();} 2. Difference: Partial can directly output content, it is inside is to convert HTML content to string character (mvchtmlstring) (HTML encoding), and then cache, and finally output to the page at once. Obviously, this conversion process will reduce the efficiency, so usually use renderpartial instead. Both of these simply crawl the partial view page content and cannot perform the partial view method, so displaying the partial view with the partial or RenderPartial method does not create a corresponding action because the action is not taken. Action and Renderaction 1.Razor syntax: @Html. Action () and @{html.renderaction ();}
2. Difference: Action is also a direct output, and the same as Partial, there is a conversion process. Renderaction output directly to the current HttpContext efficiency.
In addition to this, action accesses the action in the controller and executes the business within the action, compared to partial. Third, renderpage 1.Razor syntax: @RenderPage ()
2. Difference: You can also use Renderpage to render parts, but it cannot use the Model and ViewData of the original view, only pass through the parameters. RenderPartial and Renderaction can use the Model and ViewData of the original view. @RenderPage also did not execute action. The case of non-transfer of parameters
<! DOCTYPE html> <meta name="Viewport"Content="Width=device-width"/> <title>Index</title> <body> <div> <section> @RenderPage ("~/views/templates/partial1.cshtml") </section> </div> </body> The case of the transfer of parameters
<! DOCTYPE html> <meta name="Viewport"Content="Width=device-width"/> <title>Index</title> <body> <div> <section> @RenderPage ("~/views/templates/partial1.cshtml",New{param1="Longxi", param2="male"}) </section> </div> </body> Partial1.cshtml
@{ var param = string. Format ("{0}-{1}", pagedata["param1"], pagedata[" param2 " ]); } @Html. Raw (param)
Use of the MVC partial view (html.partial/renderpartial, html.action/renderaction, Renderpage)