Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Content for Section in Razor

I want to have some default content for a section let suppose Footer. How can I do this.

Also, what if I want to render a partial view as the default content.

Thanks

like image 875
Shakeeb Ahmed Avatar asked May 14 '11 12:05

Shakeeb Ahmed


People also ask

What is the use of section in MVC?

@section is for defining a content are override from a shared view. Basically, it is a way for you to adjust your shared view (similar to a Master Page in Web Forms).

Which keyword of Razor syntax is used to render the view content in the layout page as body content?

The @section directive is used in conjunction with MVC and Razor Pages layouts to enable views or pages to render content in different parts of the HTML page.

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


2 Answers

@RenderSection("nameOfSection", required: false)
@if (!IsSectionDefined("nameOfSection"))
{
    <div>Default section content</div>
}
like image 164
Bahtiyar Özdere Avatar answered Sep 24 '22 22:09

Bahtiyar Özdere


Haacked blogged about this http://haacked.com/archive/2011/03/05/defining-default-content-for-a-razor-layout-section.aspx.

like image 29
Necros Avatar answered Sep 21 '22 22:09

Necros