假設我在ASP.NET MVC 3應用程序中具有以下結構。爲什麼我的.cshtml頁面需要定義內容?
- 物品
- Index.cshtml
- 分類
- 共享
- _Index.cshtml
- _Site.cshtml
- Index.cshtml
兩個Index.cshtml
文件使用_Index.cshtml
作爲佈局頁面和_Index
嵌套在_Site
佈局中。
Items/Index
實現_Index
中定義的可選部分。 Shared/Index
是空的。
該Items/Index
視圖正常工作。由於類別沒有索引,因此它使用共享文件夾中的一個。這不起作用。
它拋出
的 「RenderBody」 方法沒有被調用的頁面佈局 「〜/查看/共享/ _Index.cshtml」 的錯誤。
如果_Site
電話RenderBody
,並從_Site
_Index
繼承,不_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)
筆數/ 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
雖然我的_Index.cshtml不會調用@RenderBody。 _Index只是實現_Site中的部分,並定義了一些新的部分。然後項目/索引實現一些_Index部分。而已。 – Brandon
@Brandon - 如果你包含一些基本的cshtml代碼說明,可能會有幫助,因爲我只是不明白你在描述什麼。 –
已更新的問題。也許我沒有正確實現嵌套頁面,但基本上是它的結構。 – Brandon