【问题标题】:The following sections have been defined but have not been rendered for the layout page “~/Views/Shared/_Layout.cshtml”: “featured”以下部分已定义但尚未为布局页面“~/Views/Shared/_Layout.cshtml”呈现:“featured”
【发布时间】:2016-04-25 15:29:10
【问题描述】:

我知道有很多关于这个错误的问题,并且总是有关于

的答案
@RenderSection("scripts", required: false)

但我的 _Layout.cshtml 文件中没有该部分。 我的档案:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")"></script>
</head>
<body>
    <ul id="menu">
        <li>@Html.ActionLink("Główna", "Index", "Home")</li>
        <li>@Html.ActionLink("Uzytkownicy", "Index", "Database")</li>
        <li>@Html.ActionLink("O systemie", "About", "Home")</li>
    </ul>
    <section id="main">
        @RenderBody()
        <p>Copyright 2015. All Rights Reserved.</p>
    </section>
</body>
</html>

我尝试过:

  1. 在@RenderBody() 上输入“脚本”,必需:false,
  2. 从头部删除脚本
  3. 将@RenderSection("scripts", required: false) 放在脚本上方的头部。

这些都没有帮助我。

【问题讨论】:

    标签: asp.net asp.net-mvc razor asp.net-web-api


    【解决方案1】:

    Section 就像您在布局中定义的占位符,并从您的特定视图中注入一些真实的内容。当页面运行时,您从页面部分传递的内容将被注入到您的布局中调用RenderSection() 的位置。

    看起来您的视图正在通过featured 部分传递一些东西,但您的布局没有“特色”部分的定义。所以你需要将它添加到布局文件中

    <body>
        <section id="main">
    
            @RenderBody()
    
            @RenderSection("featured", required: false)
    
        </section>
    </body>
    

    或从您的特定视图中删除featured section 代码。

    您可能需要在布局中保留@RenderSection("scripts",required:false),这将帮助您在特定视图中包含页面特定的javascript。

    【讨论】:

    • 谢谢,我从视图中删除了渲染部分,这很有帮助:)
    猜你喜欢
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多