假设我的ASP.NET MVC 3应用程序中有以下结构。
- 项目
- Index.cshtml
- 分类
- 共享
- _Index.cshtml
- _Site.cshtml
- Index.cshtml
都 Index.cshtml 文件使用 _Index.cshtml 作为布局页面和 _Index 嵌套在 _Site 布局。
Items/Index 实现在中定义的可选部分 _Index。 Shared/Index 是空的。
该 Items/Index 视图工作正常。由于Categories没有索引,因此它使用Shared文件夹中的索引。这不起作用。
它抛出了错误
尚未针对布局页面“〜/ Views / Shared / _Index.cshtml”调用“RenderBody”方法。
如果 _Site 电话 RenderBody,和 _Index 继承自 _Site,不是内容 _Index 满足要求 RenderBody 打电话和 Shared/Index.cshtml 可以空白吗?
我问的原因是因为我有一个ASP.NET MVC 1应用程序,它使用母版页实现了这个结构,并且工作正常,但是使用Razor将其转换为MVC 3导致了这个问题。
以下是我所描述的基本概要:
_Site.cshtml
<!DOCTYPE html>
// head
<body>
@RenderBody()
</body>
_Index.cshtml
@{
Layout = "~/Views/Shared/_Site.cshtml";
}
<div id="sub-menu">
// Markup
</div>
// More markup
@RenderSection("SectionOne", required: false)
@RenderSection("SectionTwo", required: false)
Items / Index.cshtml(工作)
@{
Layout = "~/Views/Shared/_Index.cshtml";
}
@section SectionOne {
// Markup
}
Shared / Index.cshtml(RenderBody错误)
@{
Layout = "~/Views/Shared/_Index.cshtml";
}
// Rest of this file is empty