【问题标题】:ASP.NET MVC rendering sections for dynamically driven content (Widgets)用于动态驱动内容(小部件)的 ASP.NET MVC 呈现部分
【发布时间】: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


【解决方案1】:

问题在于,在您的布局页面中,_DefaultTheme.cshtml,通过编写所有这些:

@RenderSection("_HeaderSection", false)
@RenderSection("_AboveBodySection", false)
@RenderSection("_BelowBodySection", false)
@RenderSection("_AfterBodySection", false)
@RenderSection("_FooterSection",false)

您断言在任何子页面中,例如_DefaultThemeArticles,都必须为上述每个页面定义一个部分。如果 Razor 未在子页面中找到定义的特定部分(在您的情况下为 _BelowBodySection),则会引发错误。您可以使用以下 Razor 来解决此问题:

@if (IsSectionDefined("_BelowBodySection"))
{
    @RenderSection("_BelowBodySection")
}

这只会在该部分存在时才呈现它。

希望这会有所帮助。

【讨论】:

  • 我明白这一点,但正如在索引页面中看到的那样,我使用 DefineSection 方法动态地写出这些。页面可以有小部件,这些小部件包含一个部分名称。部分名称必须存在,在这种情况下,它们确实存在于父布局中。 DefineSection 方法应该正确呈现这些部分吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-21
相关资源
最近更新 更多