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