Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

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

I'm new to ASP MVC and utilizing the Intro to ASP MVC 4 Beta tutorial http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

I'm encountering an error that I can't seem to find an answer to nor do I have much programming experience so I don't know where to even start to fix this an move on with the tutorial. I appreciate any help you can provide.

I'm in the Accessing Your Model's Data from a Controller section and I am getting this error when I attempt to creat a Movie as a part of the tutorial, I click on the the link "Create New" and I get the following error

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

Rather than use Visual Studio express, I opted to download Visual Studio 2012 RC (not sure if that would be the root cause of my issue.

I realize you may require me to include code to answer this but I'm not sure what code to even include. Please advise what code you need me to include if any and I will be happy to add it to my question.

Thank you,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It means that you have defined a section in your master Layout.cshtml, but you have not included anything for that section in your View.

If your _Layout.cshtml has something like this:

@RenderSection("scripts")

Then all Views that use that Layout must include a @section with the same name (even if the contents of the section are empty):

@{
    ViewBag.Title = "Title";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts{
    // Add something here
}

As an alternative, you can set required to false, then you won't be required to add the section in every View,

@RenderSection("scripts", required: false)

or also you can wrap the @RenderSection in an if block,

@if (IsSectionDefined("scripts"))
{
    RenderSection("scripts");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...