.NET MVC 4.5 - Section not defined

2019-06-27 16:06发布

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.

标签: razor
1条回答
仙女界的扛把子
2楼-- · 2019-06-27 16:38

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

查看更多
登录 后发表回答