Use of the MVC partial view (html.partial/renderpartial, html.action/renderaction, Renderpage)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.