Render.md - brainchildservices/curriculum GitHub Wiki

Slide 1

@RENDERBODY

@RenderBody is used for injecting content from child page into the master page design. If there is no named section in child page, the content will get displayed in RenderBody section.

Example:

Layout

 <div class="content">
    @RenderBody()
 </div>

Slide 2

View page content

 @{
     ViewBag.Title = "DarkLayoutPage";
     Layout = "~/Views/Shared/_MyLayout.cshtml";
 }

 <h2>DarkLayoutPage</h2>
 Hello CompShop Appliation. I am in RenderBody section because there is no named section for me.

Slide 2 Downwards

Output

image

Slide 3

@RENDERSECTION

@RenderSection is used for injecting content in the defined section. It allows you to specify a region in Layout.

Two steps are there to define @RenderSection in ASP.NET MVC.

A. Specify a @RenderSection Area in Layout Page.

 <div style="background-color:rebeccapurple; color:antiquewhite; font-weight:bold">
         @RenderSection("Note",false)
     </div>
     <!-- End of Content Body -->

     <!-- Footer -->
     <footer>
         <h4>I am Footer.</h4>
         <div style="background-color:red; color:aliceblue">@RenderSection("Footer", false)</div>
     </footer>

Slide 4

B. Use this specified section to inject content from child page.

     @{
         ViewBag.Title = "DarkLayoutPage";
         Layout = "~/Views/Shared/_DarkLayout.cshtml";
     }

     <h2>DarkLayoutPage</h2>

     Hello CompShop Application. I am in RenderBody section because there is no named section for me.

     @section Note
     {
         I am a RenderSectionArea.
     }

     @section Footer
     {
         I am Footer Section Areas.
     }
     <h2>Hello world</h2>

Note: Order doesn’t matter. You can use @section Footer before @section Note and the output would be same.

Slide 5

You have noticed that I have used false parameter when created a section on the layout page. @RenderSection("Footer", false) false parameter denotes that Footer section is optional and It is your choice whether to use it or not. If you use true parameter then it is compulsory to use this section in child page otherwise you will get following error.

[HttpException (0x80004005): Section not defined: "Footer".]

image

Ref:- https://www.completecsharptutorial.com/asp-net-mvc5/asp-net-mvc-5-renderbody-renderpage-and-rendersection-with-example.php

⚠️ **GitHub.com Fallback** ⚠️