asp.net-mvc 部分已定义但未为布局页面呈现 "~/Views/Shared/_Layout.cshtml": "head"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16436101/
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-09-08 02:48:55  来源:igfitidea点击:

Section has been defined but not rendered for the layout page "~/Views/Shared/_Layout.cshtml": "head"

asp.net-mvcasp.net-mvc-4

提问by Cybercop

I created a new MVC4 internet application. I'm just following the book ASP.NET MVC4 in Action. The chapter is Ajax is ASP.NET in MVC. the view for index goes like this

我创建了一个新的 MVC4 互联网应用程序。我只是在关注 ASP.NET MVC4 in Action 一书。本章是Ajax 是MVC 中的ASP.NET。索引的视图是这样的

@section head{
    <script type ="text/javascript"

            src="@Url.Content("~/Scripts/AjaxDemo.js")"></script>
}

@Html.ActionLink("Show the privacy policy", "PrivacyPolicy", null, new{id="privacyLink"})

<div id="privacy"></div>

and partial view contains some very basiic markup.

部分视图包含一些非常基本的标记。

<h2>Our Commitment to privacy</h2>
This is sample priavcy policy.

In AjaxDemo.js file whose path is given in index.cshtml file i have small code

在 index.cshtml 文件中给出路径的 AjaxDemo.js 文件中,我有一些小代码

$(document).ready(function() {
    $('privacyLink').click(function(event) {
        event.preventDefault();

        var url = $(this).attr('href');
        $('#privacy').load(url);
    });
});

now when I run this application and manually give the link as http://localhost:19208/customajax

现在,当我运行此应用程序并手动提供链接时 http://localhost:19208/customajax

I get error saying

我得到错误说

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml": "head".

any idea what's the mistake that I'm doing here?

知道我在这里做的错误是什么吗?

The _Layout.cshtmlis as default, I haven't changed anything to it

_Layout.cshtml是默认的,我没有改变任何东西

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")

    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title">@Html.ActionLink("your logo here", "Index", "Home")</p>
                </div>
                <div class="float-right">
                    <section id="login">
                        @Html.Partial("_LoginPartial")
                    </section>
                    <nav>
                        <ul id="menu">
                            <li>@Html.ActionLink("Home", "Index", "Home")</li>
                            <li>@Html.ActionLink("About", "About", "Home")</li>
                            <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper">
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - My ASP.NET MVC Application</p>
                </div>
            </div>
        </footer>

        @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
    </body>
</html>

回答by Satpal

You have not defined the headsection in the _Layout.cshtml

您尚未head_Layout.cshtml

You just need to defined it in the head section of _Layout.cshtml

你只需要在 head 部分定义它 _Layout.cshtml

As

作为

<head>
    @RenderSection("head", required: false)
</head>