【发布时间】:2017-03-09 17:22:21
【问题描述】:
我在 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 部分已经在母版页中定义了,为什么在这里不起作用?。
【问题讨论】:
-
它并不像您想象的那么自动。如果要在子模板中使用该部分并将其指向父模板上的相应部分,则需要在子模板中定义该部分。见stackoverflow.com/questions/8578843/…。
标签: c# asp.net asp.net-mvc razor