假定子视图:剃须刀使用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)
是唯一可以使用的公共访问方法,但它并不真正有用。
我不认为这是可能的。 – SLaks
在这里得到了一个完美的解决方案:[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) –