Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

@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

$(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

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.cshtml is as default, I haven't changed anything to it

<!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>
like image 658
Cybercop Avatar asked May 08 '13 08:05

Cybercop


1 Answers

You have not defined the head section in the _Layout.cshtml

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

As

<head>
    @RenderSection("head", required: false)
</head>
like image 142
Satpal Avatar answered Nov 05 '22 09:11

Satpal