title: ASP.NET MVC rendering sections for dynamically driven content (Widgets)
body: <p>I am receiving the following error in ASP.NET MVC. "the following sections have been defined but have not been rendered for the layout page" - Now I know what the error is about, but can't figure a way around it in fact it probably should work but I don't know why it won't. </p>
  
  <p>I have an index.cshtml page which renders all dynamically driven DB content. Pages can contain widgets (Reusable blocks on content). The index.cshtml code is below:</p>
  
  <pre><code>    @{
      Layout = AppSettings.LayoutFileDirectory + "/" + PageDAL.GetLayoutFileForPage(Model.Page);
      string currentSection = string.Empty;
  }
  
      @if(!string.IsNullOrEmpty(Model.Page.PageTitle))
      {
             &lt;h3&gt;@Model.Page.PageTitle&lt;/h3&gt;
      }
  
  
  @Html.Raw(Model.Page.PageText)
  
  
  
  @foreach (var item in Model.Page.WidgetsInPages)
  {
      //if(IsSectionDefined(item.WidgetSection))
      //{
  
          string sectionName = item.WidgetSection;
          //don't want to redefine section
          if (currentSection!= sectionName)
          {
              DefineSection(sectionName, () =&gt;
              {
                  //render all this sections widgets
                  foreach (var insider in Model.Page.WidgetsInPages.Where(w =&gt; w.WidgetSection == sectionName).OrderBy(w =&gt; w.WidgetSortOrder))
                  {
                      if (insider.Widget.WidgetFile != null)
                      {
                          Write(Html.Partial(AppSettings.WidgetTemplatesDirectory + "/" + insider.Widget.WidgetFile, item.Widget));
                          //Write(Html.Partial(AppSettings.WidgetTemplatesDirectory + "/" + insider.Widget.WidgetFile, new {Widget=item.Widget}));
                      }
  
                  }
              });
              currentSection = sectionName;
          }
  
      //}
  }
  </code></pre>
  
  <p>Now I have a _DefaultTheme.cshtml with the following sections. (This is the primary layout page)</p>
  
  <pre><code>&lt;div class="header"&gt;
        @RenderSection("_HeaderSection", false)
  &lt;/div&gt;
  
  
  
      &lt;div class="container"&gt;
          @RenderSection("_AboveBodySection", false)
          @RenderBody()
          @RenderSection("_BelowBodySection", false)
      &lt;/div&gt;
  
      @RenderSection("_AfterBodySection", false)
  
      &lt;footer class="footer"&gt;
      &lt;div class="container"&gt;
          @RenderSection("_FooterSection",false)
          @Html.Raw(FrontEnd.PopulateFooter(Model.Website))
      &lt;/div&gt;
  
      &lt;/footer&gt;
  </code></pre>
  
  <p>Adding widgets to the any page which has the _DefaultTheme layout works perfectly fine, but when I start inheriting pages it becomes an issue. I now have another page _DefaultThemeArticles.cshtml the master page is _DefaultTheme.cshtml</p>
  
  <p>The _DefaultThemeArticles.cshtml contains this:</p>
  
  <pre><code>@{ 
      Layout = "~/Themes/_DefaultTheme.cshtml";
  }
  
  
  &lt;div class="container"&gt;
      @RenderBody()
  &lt;/div&gt;
  </code></pre>
  
  <p>Now the error occurs when adding Widgets to any page which has this layout. I get this error:</p>
  
  <p>The following sections have been defined but have not been rendered for the layout page "~/Themes/_DefaultThemeArticles.cshtml": "_BelowBodySection".</p>
  
  <p>But the section _BelowBodySection has been defined in the master page, why would it not work here?.</p>