Use of asp.net MVC3 template pages (2) _ Practical Tips

Source: Internet
Author: User

This article shows you how to use the template page in the MVC3, in the traditional WebForm design mode, we use masterpage as the template page, in MVC3 Razor view design, we use another way as Template page.

Create a new MVC3 project, in solution resource management, we can see a shared folder under the Views folder. There is a _layout.cshtml page in the shared folder. This is the default template page in the project. As shown in the following illustration:

1. New content Page
Content pages are also called view layout pages in MVC3, which you can right-click to select the View folder, add a view by adding a content page, and then select the appropriate template. Here is the content page we created viewpageone.cshtml

Pages that use template pages automatically generate code in the page. To specify a template page, use layout to specify a specific template page. If there is no layout property in the content page, the default template page is used

@{ 
 viewbag.title = "Viewpageone"; 
 Layout = "~/views/shared/_layout.cshtml"; 
} 
 

If layout is specified as null, the template page is not used

@{ 
 viewbag.title = "Viewpageone"; 
 Layout = null; 

2. New Template page

Right-click to select the shared file, then select Add New item in the menu, pop the Add New Item dialog box, and then select MVC3 Layout page so that the layout page is completed, and the following layout page is added _layoutpageone.cshtml

3. Specify a template page for a file individually
If a page does not want to use the default template page, you can specify a template page for it individually, and the following code assigns the template page of the content page to the template page that you just created _layoutpageone.cshtml

@{ 
 viewbag.title = "Viewpageone"; 
 Layout= "~/views/shared/_layoutpageone.cshtml"; 
} 

4. Specify a template page for a view folder
If you want to have a controller all the views below use the same template file, you can create a _viewstart.cshtml file under the controller corresponding attempt folder, and then in the _ Viewstart.cshtml inside Specifies the template page you are using

@{ 
 Layout = "~/views/shared/_layoutpageone.cshtml"; 
} 

This allows you to designate a controller view below to use a template file

5. Page does not use template page
If a content page does not want to use a template page, you can set _layout to null
You can also not select a template when you add a view page.

6. @RenderBody ()
@RenderBody () uses the position in the template page that represents the content page in the template. When you create a content page that has a template page, the content page is rendered in the location of the @renderbody () in the template page, and only one @renderbody () can be found in a template page.

7. @RenderSection
@RenderSection is used to define an area in the template layout, where content pages can define content to populate the area, such as the JS file referenced in the content page, which can be populated to the section location of the template page. Individual information for each content page can be displayed in this area of the template page.
The @RenderSection has two parameters, the first parameter is used to define the name of the section, the 2nd argument is a Boolean type, and if true, the content page must define it, or false to indicate that the content page can define a section or be undefined.

Template page:

<! DOCTYPE html> 
 
 
 

Content page:

 @{viewbag.title = "Home page"; }  
  

8. @RenderPage
  @RenderPage is used to represent a page The contents of another page are rendered in the face. parameter specifies the location of the page to render.
  We create a new footer.cshtml file under the shared folder, write the copyright information inside, and then open a place in the template page to render the footer page.

 <!  
 DOCTYPE html>  
  

9. @Html. Partial () and html.renderpartial ()
@Html. Partial () and html.renderpartial () can be used to output a partial page, right-click to add a new item, select MVC3 Partial page, you can add a partial page, equivalent to a user control,
At this point, you can use two methods to output the content of the partial page, of course, these two methods can also directly output the view page and content page.
The parameter for @Html. Partial () is the user control name, which is the partial page name, and the return value is string and can be directly exported.
The parameter of @Html. RenderPartial () is also the user control name, the return value is void, and the content is output directly to the response when invoked.
Usage is as follows:

<div id= "Logindisplay" > 
    @Html. Partial ("_partiallogin") 
    @{ 
     html.renderpartial ("_partiallogin") ; 
     } 
     
   

That's how asp.net MVC3 template pages are used

Related Article

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.