Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET MVC 4.5 - Section not defined

Tags:

razor

I'm rendering a Razor template and would like to define sections, to which I'm able to add content throughout all included pages (namely, javascripts). However, all sections so far have come up as empty. If I set them to required: true, I get the error that the section has not been defined. To test this, I added the following code to my main template file:

@section foo {
    <p>Hello</p>    
}
@RenderSection("foo", true)

These lines are right on top of each other. The rendering of the template fails with the message Section not defined: "foo".

Given that the section is obviously defined, did I maybe miss something in the Project configuration or Controller to enable support for sections? All other @commands (like @RenderBody())inside the template seem to work fine, so some support for the Razor commands is clearly present.

like image 700
doque Avatar asked Jan 13 '23 16:01

doque


1 Answers

It looks like you're defining your section and trying to render it from your shared layout. As I understand it, you need to call RenderSection in your shared layout and then define the section it your views that use that shared layout.

This link from Scott Gu is a pretty good reference for sections:

MVC 3 Layouts & Sections

like image 137
chris.house.00 Avatar answered Jan 22 '23 15:01

chris.house.00