8

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

4

4 回答 4

3

在 Views 文件夹下的_ViewStart.cshtml中,将 Layout 值更改为您的自定义布局。我认为这可能会有所帮助..(确保您返回的是视图而不是部分视图)

例如

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

如果您想更改特定页面的布局,您可以在页面顶部将其显式定义为页面指令。

于 2012-06-11T13:38:15.153 回答
3

在 index.cshtml 中有一个在原始布局文件“_LayoutHome.cshtml”中定义的部分被称为在新的引导布局中未定义。

具体来说:@RenderSection("featured", required: false)

所以解决方案是要么将此部分添加到新布局中(在原始布局中查找并粘贴它),要么直接从 index.cshtml 中删除它。

于 2013-04-12T13:37:18.967 回答
2

我也遇到了同样的问题,我删除了

@section featured {

从视图

于 2015-05-11T11:12:34.480 回答
0

另一种方法是在 _ViewStart.cshtml 页面中使用条件块。例如,您可能有两种布局,具体取决于设备普通用户。使用伪代码读取设备/浏览器类型位,它看起来像这样:

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

我已经使用它来根据不同类别的用户的需要显示或隐藏部分或菜单项;它应该也适用于特定于设备的布局。

乔伊摩根

于 2013-05-02T13:29:18.173 回答