C# MVC @RenderSection“部分已定义但尚未呈现”脚本。当多级页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14479442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:57:45  来源:igfitidea点击:

MVC @RenderSection "sections have been defined but have not been rendered" scripts. When multiple levels of page

c#asp.net-mvcrazorasp.net-mvc-4

提问by MartinS

I am working with MVC v4.
I have a '_BootstrapLayout' page which defines all the twitter bootstrap etc stuff, A main page which defines the site layout, navbar etc, and site pages which inherit from main page.

我正在使用 MVC v4。
我有一个“_BootstrapLayout”页面,它定义了所有 twitter bootstrap 等内容,一个主页定义了站点布局、导航栏等,以及从主页继承的站点页面。

_BootstrapLayout.cshtml

_MainPage.cshtml @{ Layout = "~/Views/Shared/_BootstrapLayout.cshtml"; }

Index.cshtml @{Layout = "~/Views/Shared/_MainPage.cshtml";}

_BootstrapLayout.cshtml

_MainPage.cshtml @{ Layout = "~/Views/Shared/_BootstrapLayout.cshtml"; }

Index.cshtml @{Layout = "~/Views/Shared/_MainPage.cshtml";}

So Master Page --> Main Page --> site Page(s)

所以母版页 --> 主页 --> 站点页面

The _BootstrapLayout page contains a rendersection for scripts

_BootstrapLayout 页面包含脚本的渲染部分

@RenderSection("scripts", required: false)

I want to add a scripts section to the Index.cshtml page, but when I do I get the exception

我想向 Index.cshtml 页面添加一个脚本部分,但是当我这样做时出现异常

@section scripts {
    <script type="text/javascript">

        $(document).ready(function () {
...

        });

    </script>
}

The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_MainPage.cshtml": "scripts".

以下部分已定义但尚未为布局页面“~/Views/Shared/_MainPage.cshtml”呈现:“脚本”。

So I added an empty @section scripts to the _MainPage.cshtml, still the same problem ? Even if I add code to the _MainPage scripts section I still get the same error. It doesn't matter where I put the @section in _MainPage, still get the same error.

所以我在 _MainPage.cshtml 中添加了一个空的 @section 脚本,还是同样的问题?即使我将代码添加到 _MainPage 脚本部分,我仍然会遇到相同的错误。我把@section 放在_MainPage 的什么地方都没有关系,仍然会得到同样的错误。

If I purposely don't close the section (ie, delete the }) then I get an error which indicates that the section is incorrect, so it's parsing the section in _MainPage.

如果我故意不关闭该部分(即删除 }),则会收到一个错误,表明该部分不正确,因此它正在解析 _MainPage 中的该部分。

How can I get @RenderSections for scripts on the site pages to work in this case ?

在这种情况下,如何让 @RenderSections 用于网站页面上的脚本?

采纳答案by moribvndvs

You'll need to redefine the section in _MainPage.cshtml.

您需要重新定义_MainPage.cshtml.

_BootstrapLayout.cshtml

_BootstrapLayout.cshtml

@RenderSection("scripts", false)

_MainPage.cshtml

_MainPage.cshtml

@section scripts
{
   @RenderSection("scripts", false)
}

Index.cshtml

索引.cshtml

@section scripts
{
   <script>
     // etc.
   </script>
}