• Register
0 votes
323 views

Problem :

I am new to ASP MVC and I am facing one error. I am trying to access the Model's Data from the Controller section and I am getting following error when I try to create a Movie as a part of the exercise, whenever I try to click on the link "Create New" I face the below error

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

I am using the Visual Studio 2012 RC I am not sure if this is the root cause of my issue.Please let me know the solution for my issue.

7.5k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

I think you have defined the section in your master Layout.cshtml, however you forgot to included anything for the section in your View.

If suppose your _Layout.cshtml has following code:

@RenderSection("scripts")

Then all the Views that use that Layout should include the @section with the same name as follows:

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

Or you can just set required to false and then you will not be required to add your section in every View as follows:

@RenderSection("scripts", required: false)

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

@if (IsSectionDefined("scripts"))
{
    RenderSection("scripts");
}
38.6k points
0 votes

Solution:

It implies that you have identified a section in your master Layout.cshtml, however you have not added anything for that section in your View.

In case your _Layout.cshtml has something like this:

@RenderSection("scripts")

Then all Views that employ that Layout should add a @section with the similar 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 substitute, you can place required to false, then you won't be required to include the section in every View,

@RenderSection("scripts", required: false)

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

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

Furthermore, you can include the following line to the _Layout.cshtml or _Layout.Mobile.cshtml:

@RenderSection("scripts", required: false)

I had a instance with 3 levels a'la _MainLayout.cshtml <--- _Middle.cshtml <--- Page.cshtml. Even though doing like this

_MainLayout.cshtml

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

_Middle.cshtml

@section head {
    @RenderSection("head")
}

and in Page.cshtml identifying

@section head {
   ***content***
}

I would still obtain the error

The pursuing sections have been identified however have not been rendered for the layout page “~/Views/Shared/_Middle.cshtml”: "head".

Turned out, the error was for the Middle.cshtml to depend on /Views/_ViewStart.cshtml to resolve it's parent layout. The problem was resolved by identifying this in Middle.cshtml explicitly:

@{
Layout = "~/Views/_Shared/_MainLayout.cshtml";
}

 

31.7k points
edited by