• Register
0 votes
140 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.

6 5 3
7,540 points

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");
}
9 7 4
38,600 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";
}

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 79 views
79 views
Problem : I have recently installed a website on my local machine using IIS 7 successfully. But when I tried to deploy it on the live server, I got a following error: "The page cannot be displayed because an internal server error has occurred" Nothing else. Using a same IIS 7 on live and also set to have the Detailed errors in Error Pages module, but still getting a same. What can be the reason?
asked Jan 28 jwilliam 3.9k points
0 votes
1 answer 10 views
10 views
Problem: I am naive to Asp.Net.I am having a weird looking problem where I am trying to open up my ASP.NET MVC application but I am receiving the below ASP.NET error page which says: Server Error in '/' Application. The resource cannot be found. Description: ... unknown reason to me it seems like it is actually searching for the .aspx file. Please guide me if you think I am missing something.
asked Aug 19 Raphael Pacheco 4.9k points
0 votes
1 answer 50 views
50 views
Problem : I have my newly created website. First time I was able to succesfully login. The default address: www.abc.com I typed above address on browser and it redirected me to my login page: www.abc.com/pages/landingpage.aspx When I entered my login credential and tried to log ... open my site number of tabs and the browsers but as soon as I am logged in to my site, I am getting the above error.
asked Jan 31 jwilliam 3.9k points
0 votes
1 answer 47 views
47 views
Problem : I am very new to ASP.Net. I have created my first ASP.Net application which impersonates the user in order to create the AD group, and after that it launches the powershell process as the user saperately. But because of some reason the group ... I would rather not compromise with the security policy with the server ideally, and this application will be used by around 100+ users.
asked Dec 5, 2019 alecxe 7.5k points
0 votes
2 answers 212 views
212 views
Problem : Currently I am migrating from the Identity 1.0.0 to Identity 2.0.1. But the migrations code generated is nothing about the new IdentityUser. And It does not even add the new columns. So I tried to make the new project and also I ... current context At below line IdentityResult result = await UserManager.CreateAsync(user, model.Password); Any clue about the solution on my above problem?
asked Dec 25, 2019 alecxe 7.5k points