Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RenderBody() and RenderSection() must be on every child layout?

Tags:

I have three simple layout,

_Layout.cshtml (this is the base layout)

@RenderSection("something", required: false) @RenderBody() 

_Main.cshtml

@{     Layout = "~/Views/Shared/_Layout.cshtml"; } @section something {    Hey I'm actually on the _Main layout. } 

Index.cshtml

@{     Layout = "~/Views/Shared/_Main.cshtml"; } 

When I try to render Index view in an action, I got this error,

The "RenderBody" method has not been called for layout page "~/Views/Shared/_Main.cshtml".

But wait, _Main.cshtml has a parent layout which already has a RenderBody(). So am I wrong, must I call RenderBody() for every child layout?

like image 609
Okan Kocyigit Avatar asked Aug 19 '13 21:08

Okan Kocyigit


People also ask

What is RenderBody and RenderSection in MVC?

RenderBody() renders all the content of the child view which is not wrapped in the named section. RenderSection() renders only a part of the child view which is wrapped under the named section. Multiple RenderBody() methods are NOT allowed in a single layout view.

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 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"> .

Can we have multiple _ViewStart in MVC?

We can also create multiple _ViewStart. cshtml pages. The file execution is dependent upon the location of the file within the folder hierarchy and the view being rendered. The MVC Runtime will first execute the code of the _ViewStart.


2 Answers

Yes, RenderBody should be included on every layout page, regardless the nesting.

@RenderBody works as a placeholder for the engine to know where to drop the content of the view using the layout page.

like image 132
Raciel R. Avatar answered Oct 07 '22 01:10

Raciel R.


This code should work properly:

_Layout.cshtml

@RenderSection("something", required: false) @RenderBody() 

_Main.cshtml

@{     Layout = "~/Views/Shared/_Layout.cshtml";  } @section something {    Hey I'm actually on the _Main layout. } 

Index.cshtml

@{     Layout = "~/Views/Shared/_Main.cshtml";  } <div id="Index Content Here">  @RenderBody()  </div> 

index.cshtml should be rendered as per below:

<head> Hey I'm actually on the _Main layout.    </head>  <div id="Index Content Here"> </div> </div> 
like image 24
Girish Gupta Avatar answered Oct 07 '22 01:10

Girish Gupta