C # Introduction to the Layout View of Mvc.net 3.0 Razor Leyout

Source: Internet
Author: User

The test requires a home page entry point. Here, you can use the default route settings to test.

1. First, create an action in the default homecollector, as shown below:

Public ActionResult index ()
{
Return View ();
}

 

2. Create the MyLeyout. cshtml view under the view/home file. The content is as follows:

@{
// Some code
}
<! DOCTYPE html>

<Html>
<Head>
<Title> @ ViewBag. Title </title>
</Head>
<Body>
<Div>
Start rendering Body <br/>
@ RenderBody ()
Rendering Body ends <br/>
<Br/>
Start rendering ViewPage1 <br/>
@ RenderPage ("~ /Views/Home/ViewPage1.cshtml ")
End rendering ViewPage1 <br/>
<Br/>
Start rendering ViewPage2 <br/>
@ RenderPage ("~ /Views/Home/ViewPage2.cshtml ")
End rendering ViewPage2 <br/>
<Br/>
Learned Section usage <br/>
Start rendering Section <br/>
Declaration method 1 (recommended): SectionA: <br/>
@ * The reference page is neither marked nor abnormal. If the second parameter is true, the reference page must have the SectionA tag *@
@ RenderSection ("SectionA", false)
------- <Br/>

Declaration Method 2: SectionB: <br/>
@{
// Determine whether SectionB is marked
If (IsSectionDefined ("SectionB "))
{
@ RenderSection ("SectionB ")
}
}
------- <Br/>

Declaration method 3: SectionC: <br/>
@{
// When we test the undefined SectionC, an exception will occur on the page.
// @ RenderSection ("SectionC") // open the comment during the test
}
------- <Br/>
Rendering Sction ended <br/>
</Div>
</Body>
</Html>


 

3. Create an index. cshtm file in the view/home folder. The content is as follows:

 

@{
ViewBag. Title = "Homepage ";
Layout = "~ /Views/home/MyLeyout. cshtml ";
}
<! --
Render the entire page ~ The RenderBody () placeholder in/Views/EBusiness/MyLeyout. cshtml
-->
<Div>
Here is the rendering Body.
</Div>

<! --
~ The siction definition required by the RenderSection ("SectionA", false) placeholder in/Views/EBusiness/MyLeyout. cshtml
-->
@ Section SectionA {
<Div> here is SectionA: declared on the index page </div>
}

<! --
~ The siction definition required for the RenderSection ("SectionB") placeholder in/Views/EBusiness/MyLeyout. cshtml. Use IsSectionDefined ("SectionB") to determine
-->
@ Section SectionB {
<Div> here is SectionB: it is also declared on the index page </div>
}

<! --
~ The RenderSection ("SectionC") placeholder in/Views/EBusiness/MyLeyout. cshtml,
It is not defined here. If the code comment is opened, an exception is reported, so that it is not used to determine whether a placeholder exists.
-->

4. Create the ViewPage1.cshtml view as above

 

@{
// ViewBag. Title = "Homepage ";
Layout = null;
}
<! --
Render the entire page ~ The RenderPage () placeholder in/Views/EBusiness/MyLeyout. cshtml. The RenderPage placeholder can be used multiple times.
-->
<Div>
Here we will render all the content of viwpage1.chatml.
</Div>
 

5. Create the ViewPage2.cshtml view at the same position as above to prove that the RenderPage () placeholder can be declared multiple times.

 

@{
// ViewBag. Title = "Homepage ";
Layout = null;
}
<! --
Render the entire page ~ The RenderPage () placeholder in/Views/EBusiness/MyLeyout. cshtml. The RenderPage placeholder can be used multiple times.
-->
<Div>
This is how viwpage2.chatm2 is rendered.
</Div>
Looking at how much code is messy, you should be able to understand the comments.

 

 

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.