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> }
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.
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.
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"> .
@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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With