.NET MVC 4.5 - 部分未定义 [英] .NET MVC 4.5 - Section not defined

查看:34
本文介绍了.NET MVC 4.5 - 部分未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在渲染一个 Razor 模板并想定义部分,我可以在所有包含的页面(即 javascripts)中添加内容.但是,到目前为止,所有部分都显示为空.如果我将它们设置为 required: true,我会收到该部分尚未定义的错误.为了测试这一点,我在主模板文件中添加了以下代码:

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)

这些行就在彼此的顶部.模板渲染失败并显示消息 Section not defined: "foo".

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

鉴于该部分已明确定义,我是否可能错过了项目配置或控制器中的某些内容以启用对部分的支持?模板中的所有其他 @commands(如 @RenderBody())似乎都可以正常工作,因此显然存在对 Razor 命令的一些支持.

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.

推荐答案

看起来您正在定义您的部分并尝试从您的共享布局呈现它.据我了解,您需要在共享布局中调用 RenderSection,然后在您的视图中定义使用该共享布局的部分.

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.

来自 Scott Gu 的这个链接对于部分来说是一个很好的参考:

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

MVC 3 布局 &部分

这篇关于.NET MVC 4.5 - 部分未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆