DEV Community

Hòa Nguyễn Coder
Hòa Nguyễn Coder

Posted on

ASP.NET MVC 5 Render Sections with RenderSection

RenderSection: is a method that helps us determine the display position of content on websites.
@RenderSection ("Footer", required: false): the meaning of the content displayed will not be required
@RenderSection ("Footer", required: true): required content, if the View does not declare the name Section , we will get an error

Example
_LayoutPage.cshtml

<body>
  <div>
        @RenderBody()
    </div>
    <footer>
          @RenderSection("Footer", required: false);
    </footer>
     @RenderSection("Scripts", required: false);
</body>
Enter fullscreen mode Exit fullscreen mode

Index.cshtml

@section Footer{
    <p>This is Footer Index</p>
}
@section Scripts{
    <script type="text/javascript" src="~/js/jquery.min.js"></script>
    <script>
        $(document).ready(function(){

        });
    </script>
}
Enter fullscreen mode Exit fullscreen mode

You can use the following method to check on whether the User has declared the Section Name , IsSectionDefined () : use the Check Name section.

@if (IsSectionDefined("Footer"))
  {
     @RenderSection("Footer", required: false);
  }
  else
  {
     <span>This is The default Footer</span>
   }
Enter fullscreen mode Exit fullscreen mode

Post:ASP.NET MVC 5 Render Sections with RenderSection

Top comments (0)