1. The master page _ layout. cshtml is similar to the. master file in the traditional webform and serves as the target for reuse of the overall page framework.
1. Dashboard Page code Preview
1 <!DOCTYPE html> 2
2. subpage title settings
Although multiple child pages can reference the same parent page, different page titles can be set separately.
@ Viewbag. title is a placeholder for a title. assign a value to the variable of the title in control or on the page.
1 // layoutdemo_01 2 Public actionresult layoutdemo_01 () 3 {4 viewbag. title = "layout page 1"; 5 return view (); 6} 7 8 // layoutdemo_02 9 Public actionresult layoutdemo_02 () 10 {11 viewbag. title = "layout page 2"; 12 Return view (); 13}
1 @ {2 viewbag. Title = "layout page 1"; 3}
3. Configure the main content of the subpage
The main content of the page is identified by @ renderbody. The content of the sub-page is directly replaced with this method.
1 // The first page 2 @ {3 layout = "~ /Views/shared/_ layout. cshtml "; 4} 5 here is the content of layoutg 1 6 7 8 // The second page 9 @ {10 layout = "~ /Views/shared/_ layout. cshtml "; 11} 12 here is the content of layout 2
4. settings of other content on the subpage
The following code shows that most of the content on the motherboard is not continuous:
1 <! Doctype HTML> 2 <HTML> 3
At this point, we use @ rendersection to define the placeholder sub-page implementation. The @ rendersection method accepts two parameters: ("name", "whether it is required "), if required, the sub-page must implement this method; otherwise, an error is returned.
The subpage code is as follows:
1 @ {2 Layout = "~ /Views/shared/_ layout. cshtml "; 3} 4 here is the content of layoutg 1 5 @ section masterpart {6 here is the second part of the motherboard page 7}
@ Section + space + name {content} to replace the content on the dashboard page.
5. Other considerations
The name of the dashboard page is optional, but it is recommended to start.
6. Overall Running Effect
2. Custom Controls
Create as a patial View
After creation, enter the following code in the page:
The time code is as follows:
1 @ {2 Layout = "~ /Views/shared/_ layout. cshtml "; 3} 4 @ html. Partial ("~ /Views/shared/_ usercontrol. cshtml ") 5 here is the content of layoutg 1
The running effect is as follows:
3. Default layout reference (_ viewstart. cshtml)
When layout is not specified for our page,
If _ viewstart. cshtml exists globally or in the same folder, the layout of the page automatically inherits from _ viewstart. cshtml,
If _ viewstart. cshtml exists at different folder levels, it inherits the one closest to the page.
_ Viewstart. cshtml is a special object.
1. Use global _ viewstart. cshtml
_ Viewstart. cshtml file location and content, as shown in:
In the user folder, create the page layoutdemo_03.cshtml, as shown in:
Layout is not defined in layoutdemo_03.cshtml, but in fact its layout inherits _ viewstart. cshtml:
1 @{2 Layout = "~/Views/Shared/_Layout.cshtml";3 }
The running effect is as follows:
2. Use _ viewstart. cshtml in the folder
Create the file _ layoutnew. cshtml in the shared folder, as shown in:
We added the different text "new layout" above, and then created the file _ viewstart. cshtml in the user folder, as shown in:
Modify the content of _ viewstart. cshtml in the user folder to layout = "~ /Views/shared/_ layoutnew. cshtml "; when we browse layoutdemo_03.cshtml again, the reference of the layout of the page inherits the _ viewstart in the user folder. cshtml:
Note: If you do not want the page to use any motherboard page or layout page, you need to set layout = NULL, as shown below:
1 @{2 Layout = null;3 }Copyright: http://www.cnblogs.com/iamlilinfeng
Use of layout in the view (for conversion)