[英]ASP.NET MVC rendering sections for dynamically driven content (Widgets)
I am receiving the following error in ASP.NET MVC. 我在ASP.NET MVC中收到以下错误。 "the following sections have been defined but have not been rendered for the layout page" - Now I know what the error is about, but can't figure a way around it in fact it probably should work but I don't know why it won't.
“以下部分已定义,但尚未为布局页面呈现” - 现在我知道错误是什么,但无法找到解决方法实际上它可能应该工作但我不知道为什么它惯于。
I have an index.cshtml page which renders all dynamically driven DB content. 我有一个index.cshtml页面,它呈现所有动态驱动的DB内容。 Pages can contain widgets (Reusable blocks on content).
页面可以包含小部件(内容上的可重用块)。 The index.cshtml code is below:
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;
}
//}
}
Now I have a _DefaultTheme.cshtml with the following sections. 现在我有一个带有以下部分的_DefaultTheme.cshtml。 (This is the primary layout page)
(这是主要布局页面)
<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>
Adding widgets to the any page which has the _DefaultTheme layout works perfectly fine, but when I start inheriting pages it becomes an issue. 将窗口小部件添加到具有_DefaultTheme布局的任何页面都可以正常工作,但是当我开始继承页面时,它就成了一个问题。 I now have another page _DefaultThemeArticles.cshtml the master page is _DefaultTheme.cshtml
我现在有另一页_DefaultThemeArticles.cshtml,主页是_DefaultTheme.cshtml
The _DefaultThemeArticles.cshtml contains this: _DefaultThemeArticles.cshtml包含以下内容:
@{
Layout = "~/Themes/_DefaultTheme.cshtml";
}
<div class="container">
@RenderBody()
</div>
Now the error occurs when adding Widgets to any page which has this layout. 现在,将Widgets添加到具有此布局的任何页面时都会发生错误。 I get this error:
我收到此错误:
The following sections have been defined but have not been rendered for the layout page "~/Themes/_DefaultThemeArticles.cshtml": "_BelowBodySection". 以下部分已定义,但尚未针对布局页面“〜/ Themes / _DefaultThemeArticles.cshtml”:“_ BelowBodySection”进行渲染。
But the section _BelowBodySection has been defined in the master page, why would it not work here?. 但是_BelowBodySection部分已在母版页中定义,为什么它在这里不起作用?
The problem is that in your layout page, _DefaultTheme.cshtml
, by writing all of these: 问题是在你的布局页面中,
_DefaultTheme.cshtml
,写下所有这些:
@RenderSection("_HeaderSection", false)
@RenderSection("_AboveBodySection", false)
@RenderSection("_BelowBodySection", false)
@RenderSection("_AfterBodySection", false)
@RenderSection("_FooterSection",false)
You are asserting that in any child page, like _DefaultThemeArticles
, there must be a section defined for each of the above. 您声称在任何子页面中,如
_DefaultThemeArticles
,必须为上述每个子页面定义一个部分。 If Razor doesn't find a particular section defined (in your case _BelowBodySection
) in the child page, it throws the error. 如果Razor未在子页面中找到定义的特定部分(在您的情况下为
_BelowBodySection
),则会抛出错误。 You can use the following Razor to solve this: 您可以使用以下Razor来解决此问题:
@if (IsSectionDefined("_BelowBodySection"))
{
@RenderSection("_BelowBodySection")
}
This will only render the section if it exists. 这将仅呈现该部分(如果存在)。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.