Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining if a RenderSection() was called in Layout page

Is there a way in the Layout to determine if this will render content?

@RenderSection("Right", required: false)

That is determine if there is actually content in the View to place in the section.

like image 604
Paul Avatar asked Dec 16 '10 17:12

Paul


People also ask

What is the difference between RenderBody and RenderSection?

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.

What is RenderBody () in asp net core?

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 RenderBody method in a view?

RenderBody method exists in the Layout page to render child page/view. It is just like the ContentPlaceHolder in master page. A layout page can have only one RenderBody method.

What is the use of RenderBody ()?

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.


1 Answers

Probably not exactly answering your question about testing if the section will render some content but you could test whether a section is defined and render it or provide a default content if this section is not defined:

@if (IsSectionDefined("Right")) { 
    @RenderSection("Right")
}
else { 
    <div>Default content</div>
}
like image 188
Darin Dimitrov Avatar answered Sep 30 '22 18:09

Darin Dimitrov