John Truth
John Truth

Reputation: 81

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

I know there are a few questions that have been answered but I didn't find something specific to my case.

I'm using the mobile capabilities of MVC4. So I created a _layout.mobile.cshtml and the corresponding views.

The error above happens when I go in with a mobile device. As you can see, it is trying to display the regular _layout.cshtml instead of the _layout.mobile.cshtml. So I'm assuming it is also trying to display the view (say Index.mobile.cshtm) which doesn't have the section in question. Basically it is mixing the regular layout with the mobile views.

This doesn't happen all the time. If I recycle the pool it works again for a while and then all of the sudden it goes back to having the error and it will continue until I recycle the pool again.

Has anyone seen this problem before that can shed some light?

Thanks John

Upvotes: 8

Views: 15839

Answers (4)

Alam Usmani
Alam Usmani

Reputation: 103

I had also face the same problem I removed

@section featured {

From View

Upvotes: 2

Joseph Morgan
Joseph Morgan

Reputation: 161

Another way to do this is to use a conditional block in your _ViewStart.cshtml page. For example, you may have two layouts depending on the device regular user. Using pseudo-code for the reading of the device/browser type bit, it would look something like this:

@{
        if(userIsMobile)
        {
            Layout = "~/Views/Shared/_MobileLayout.cshtml";
        }
        else
        { 
             Layout = "~/Views/Shared/_Layout.cshtml";
        }
}

I have used this to display or hide sections or menu items as needed for different classes of user; it should work as well for device-specific layouts.

Joey Morgan

Upvotes: 0

Rami Sarieddine
Rami Sarieddine

Reputation: 5432

in the index.cshtml there is a section being called defined in the original layout file "_LayoutHome.cshtml" that is not defined in the new bootstrap layout.

specifically: @RenderSection("featured", required: false)

So the solution is to either add this section to the new layout (look for it in the original layout and paste it) or simply delete it from the index.cshtml.

Upvotes: 3

amesh
amesh

Reputation: 1319

In the _ViewStart.cshtml available under the Views folder, change the Layout value to your custom layout. I think this may help.. (Make sure that you are returning View instead of partial view)

for example

@{
Layout = "~/Views/Shared/_layout.mobile.cshtml";
}

In case if you want to change the layout for a specific page you can explicitly define it at the top of the page as a page directive.

Upvotes: 3

Related Questions