2017-03-09 92 views
1

我在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节已经在母版页中定义了,为什么它在这里不起作用?

+1

它不像您想象的那样自动。您需要在子模板中定义该部分,如果您要在该模板中使用它并将其指向父模板上的相应部分。请参阅http://stackoverflow.com/questions/8578843/rendersection-in-nested-razor-templates。 – Jasen

回答

1

的问题是,在你的页面布局,_DefaultTheme.cshtml,通过写所有这些:

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

你断言,在任何子页面,如_DefaultThemeArticles,必须有每个限定的区间以上。如果Razor没有在子页面中找到特定的部分(在您的案例中为_BelowBodySection),则会引发错误。你可以使用下面的剃刀来解决这个问题:

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

这只会渲染该部分,如果它存在。

希望这会有所帮助。

+0

我明白,但如索引页面所示,我使用DefineSection方法将这些动态写出。页面可以具有小部件,并且这些小部件包含部分名称。部分名称必须存在,在这种情况下,它们确实存在于父布局中。 DefineSection方法应该使这些部分正确? – user7321559