Take the 2-minute tour ×

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

For example (code taken from example)

        <meta charset="utf-8" />
        <title>My WebSite</title>
            #container { width: 700px; }
            #left { float: left; width: 150px; }
            #content { padding: 0 210px 0 160px; }
            #right { float: right; width: 200px; }
            .clear { clear: both; }
        <div id="container">
            <div id="left">
                @RenderSection("left", required:false)
            <div id="content">
            <div id="right">
                @RenderSection("right", required:false)
            <div class="clear"></div>

      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)

@section Body{
    <h1>Body Content</h1>
share|improve this question
add comment

3 Answers

up vote 10 down vote accepted

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.

share|improve this answer
Thanks for clarifying that RenderBody's role was only for a matter of convenience. –  Prabhu Jan 29 '13 at 3:59
add comment

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!
share|improve this answer
Thanks for the input. I was wondering why RenderBody was included in the framework to start with, when RenderSection could simply be used instead. However, as @Basarat Ali mentioned, it seems like it was added for convenience. –  Prabhu Jan 29 '13 at 4:01
this was a good question. Not enough attention is paid to it. –  Dave A Jan 29 '13 at 4:05
add comment

RenderBody is required, as it's what renders each view. RenderSection has an optional parameter that lets you mark the section as not required.

share|improve this answer
Thanks, my question though was why can't RenderSection be used for the body as well--why do we need another directive...the body is after all a section of the page. –  Prabhu Jan 29 '13 at 4:04
Seems like that would just add more overhead to each view, since everything would have to exist in one section or another, much like WebForms pages that used master pages needed to have all their content in a contentplaceholder or three. I personally prefer the implied body section. –  Tieson T. Jan 30 '13 at 2:16
add comment

Your Answer


