ASP. NET MVC Partial view

Source: Internet
Author: User

"Partial View"

Some of the views in ASP. NET MVC are equivalent to the User Control in the Web Form. Our pages tend to have a lot of reuse where they can be packaged for reuse. Using partial Views: 1. You can abbreviate the code. 2. The page code is clearer and better maintained.

"How to use"

There are several ways to load a partial view in a view, including:

The Partial () Action () renderpartial () renderaction () Renderpage () method.

The following are the differences between these methods:

Partial and RenderPartial methods

1. Razor syntax: @Html. Partial () and @{html.renderpartial ();}

2. Difference: Partial can directly output the content, it is inside the HTML content is converted to string character (mvchtmlstring), and then cached, and finally output to the page at once. Obviously, this conversion process will reduce the efficiency, so usually use renderpartial instead.

How to use: Large view directly write: @Html. Partial ("mypartial") @* here the mypartial is just a partial view of the file name mypartial.cshtml*@

RenderPartial and Renderaction method

1. Razor syntax: @{html.renderpartial ();} With @{html.renderaction ();}

2. Difference: RenderPartial does not need to create the Controller's action, and renderaction needs to create the action to load in the controller.

Renderaction will call Contorller's Action first and then render the view, so the page will start a link.

If this part of the view is just some simple HTML code, please use RenderPartial. But if this part of the view needs to be rendered by reading the data in the database, in addition to the HTML code, it is necessary to use renderaction because it can call the method in the Action to read the database, render the view after rendering, and Re Nderpartial has no Action, so it can't be done.


Renderaction and Action

1. Razor syntax: @{html.renderaction ();} With @Html. Action ();

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.

Note the use of the Renderaction method: In the large view, the correct use: @{html.renderaction ("ActionName", "Controllername");}, you cannot use @html.renderaction (" ActionName "," Controllername ")


Renderpage and RenderPartial method

1. Razor syntax: @{html.renderpartial ();} With @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 can use the Model and ViewData of the original view.

ASP. NET MVC Partial view

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.