我在ASP.NET MVC中收到以下錯誤。 「以下部分已被定義,但尚未呈現爲佈局頁面」 - 現在我知道錯誤是關於什麼的,但無法解決它的問題,事實上它可能應該工作,但我不知道爲什麼慣於。動態驅動內容的ASP.NET MVC呈現部分(小部件)
我有一個index.cshtml頁面,呈現所有動態驅動的數據庫內容。頁面可以包含小部件(內容上的可重用塊)。 index.cshtml代碼如下:
@{
Layout = AppSettings.LayoutFileDirectory + "/" + PageDAL.GetLayoutFileForPage(Model.Page);
string currentSection = string.Empty;
}
@if(!string.IsNullOrEmpty(Model.Page.PageTitle))
{
<h3>@Model.Page.PageTitle</h3>
}
@Html.Raw(Model.Page.PageText)
@foreach (var item in Model.Page.WidgetsInPages)
{
//if(IsSectionDefined(item.WidgetSection))
//{
string sectionName = item.WidgetSection;
//don't want to redefine section
if (currentSection!= sectionName)
{
DefineSection(sectionName,() =>
{
//render all this sections widgets
foreach (var insider in Model.Page.WidgetsInPages.Where(w => w.WidgetSection == sectionName).OrderBy(w => w.WidgetSortOrder))
{
if (insider.Widget.WidgetFile != null)
{
Write(Html.Partial(AppSettings.WidgetTemplatesDirectory + "/" + insider.Widget.WidgetFile, item.Widget));
//Write(Html.Partial(AppSettings.WidgetTemplatesDirectory + "/" + insider.Widget.WidgetFile, new {Widget=item.Widget}));
}
}
});
currentSection = sectionName;
}
//}
}
現在我有一個_DefaultTheme.cshtml與以下部分。 (這是主要的佈局頁)
<div class="header">
@RenderSection("_HeaderSection", false)
</div>
<div class="container">
@RenderSection("_AboveBodySection", false)
@RenderBody()
@RenderSection("_BelowBodySection", false)
</div>
@RenderSection("_AfterBodySection", false)
<footer class="footer">
<div class="container">
@RenderSection("_FooterSection",false)
@Html.Raw(FrontEnd.PopulateFooter(Model.Website))
</div>
</footer>
添加小部件到具有_DefaultTheme佈局的任何網頁工作完全正常,但是當我開始繼承頁面就變成一個問題。我現在有另一個頁面_DefaultThemeArticles.cshtml母版頁是_DefaultTheme.cshtml
的_DefaultThemeArticles.cshtml包含此:
@{
Layout = "~/Themes/_DefaultTheme.cshtml";
}
<div class="container">
@RenderBody()
</div>
將小工具添加到具有這種佈局的任何頁面時,現在出現錯誤。我得到這個錯誤:
以下部分已被定義,但尚未呈現佈局頁面「〜/ Themes/_DefaultThemeArticles.cshtml」:「_BelowBodySection」。
但是_BelowBodySection節已經在母版頁中定義了,爲什麼它在這裏不起作用?
它不像您想象的那樣自動。您需要在子模板中定義該部分,如果您要在該模板中使用它並將其指向父模板上的相應部分。請參閱http://stackoverflow.com/questions/8578843/rendersection-in-nested-razor-templates。 – Jasen