2017-03-09 94 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