Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between RenderBody and RenderSection

Tags:

In MVC/Razor syntax, I'm trying to understand why we need @RenderBody.

For example (code taken from example)

<html>     <head>         <meta charset="utf-8" />         <title>My WebSite</title>         <style>             #container { width: 700px; }             #left { float: left; width: 150px; }             #content { padding: 0 210px 0 160px; }             #right { float: right; width: 200px; }             .clear { clear: both; }         </style>     </head>     <body>         <div id="container">             <div id="left">                 @RenderSection("left", required:false)             </div>             <div id="content">                 @RenderBody()             </div>             <div id="right">                 @RenderSection("right", required:false)             </div>             <div class="clear"></div>         </div>     </body> </html>   @{       Layout = "~/_3ColLayout.cshtml"; }  <h1>Main Content</h1>  @section left {     <h1>Left Content</h1> }  @section right {     <h1>Right Content</h1> } 

Why can't I simply use @RenderSection for everything, like this:

<div id="content">      @RenderSection("Body", required:true) </div>  @section Body{     <h1>Body Content</h1> } 
like image 386
Prabhu Avatar asked Jan 29 '13 03:01

Prabhu


People also ask

What is use of RenderBody RenderSection and RenderPage in MVC?

A layout page can only contain one RenderBody method, but can have multiple sections. To create a section you use the RenderSection method. The difference between RenderSection and RenderPage is RenderPage reads the content from a file, whereas RenderSection runs code blocks you define in your content pages.

What is RenderBody?

RenderBody is used for rendering the content of the child view. In layout pages, it renders the portion of a content page. It takes the content of the child page and merges into the layout.

What is the function of RenderBody ()?

RenderBody() is called to render the content of a child view. Any content on said view that is not in a @section declaration will be rendered by RenderBody() . Using the Layout view above, that means that all content in a child view will be rendered inside the <div class="container body-content"> .

What is @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.


2 Answers

Start with @RenderBody, this is vital. Your _layout has to have it. This is where your view will be rendered. If you leave it out, your app will die (I think on run time, as Views are not compiled).

[Correction: Without Renderbody, the View referencing this particular layout will die on run-time. (Important to note that layout are themselves optional.)]

Sections are code blocks defined within your View with similar names

@RenderSection("Navbar", required: false)

could have a corresponding code block in your View.

@section Navbar{     <!-- Content Here --> } 

I emphasize could because the Navbar is delcared required: false

Sections are a way each View can share a piece of functionality / markup with the _layout.

Followup: In my modest time of MVC development I have learned to make modest use of sections.

  • Sections are useful for making sure your JS references are placed in your HTML section (even though this is an antiquated practice.
  • Sections are useful for top and side navs
  • Sections never be required. To do so makes your code fragile!
like image 148
Dave Alperovich Avatar answered Sep 21 '22 02:09

Dave Alperovich


Simply because of convenience. Rendering the body is something that you would most likely do so its good to have a dedicated function for that. Keeps you from declaring a @section for the body and gives an easier to call function.

like image 21
basarat Avatar answered Sep 18 '22 02:09

basarat