2012-06-15 22 views
1

假定子視圖:剃鬚刀使用RenderSection當你不知道它的名字

@Section Section1 { 
    @:Section1 Stuff 
} 

@Section Section2 { 
    @:Section2 Stuff 
} 

@Section ExtraSection { 
    @:Extra Section Stuff 
} 

我如何設置主視圖,以便SECTION1和第2節到自己的位置,和所有其餘的獲得以統一的方式處理? (例如像一個@RenderAllOtherSections())

@RenderSection("Section1") 
@RenderSection("Section2") 
@RenderBody() 
@RenderAllOtherSections() // ExtraSection is rendered here. How? 

更新:檢查剃刀視圖網頁的基礎對象後,我已經發現,存在與在先前被定義的部分的字典(調用)視圖。它位於PreviousSectionWriters參數中,但這有一個私人獲取。另外,這本字典實際上是SectionWritersStack堆棧中的第二個項目,但堆棧也有私有獲取。已經渲染的部分在HashSet<string> _renderedSections中存儲爲「完成」,這也是私有的。

總之,我需要訪問從WebPageBase的東西是:

public abstract class WebPageBase : WebPageRenderingBase 
{ 
     private HashSet<string> _renderedSections 

     Dictionary<string, SectionWriter> PreviousSectionWriters // private get, its the 2nd item in SectionWritersStack 
    Stack<Dictionary<string, SectionWriter>> SectionWritersStack // this would do too, but private get 
} 

所以,現在的問題是,我能做些什麼才能夠訪問這些屬性?輔助方法bool IsSectionDefined(string name)是唯一可以使用的公共訪問方法,但它並不真正有用。

+0

我不認爲這是可能的。 – SLaks

+0

在這裏得到了一個完美的解決方案:[Asp.NET MVC Forums](http://forums.asp.net/p/1815167/5029823.aspx/1?Re%20In%20razor%20master%20viewpage%20how%20to%20get %20a%20list%20of%20defined%20rendered%20sections%20) –

回答

1

這是不可能的。 如果這樣的事情是可能的,主頁面將會明白哪一部分是第一,第二......等等。

,如果im瞭解你的情況,你需要在@RenderBody(後呈現段) Alternativle你可以有這樣的事情:

Master page 
@RenderSection("Section1") 
@RenderSection("Section2") 
@RenderBody() 
@RenderSection("PostBodySection", false) // The second parameter - false means that this section is not mandatory 

而在子視圖,你可以選擇任何具有財產以後在「PostBodySection」或不。

+0

感謝您的回答,但我無法接受這是不可能的。我用我發現的一些東西來更新這個問題 –