【发布时间】:2012-06-15 14:10:49
【问题描述】:
假设子视图:
@Section Section1 {
@:Section1 Stuff
}
@Section Section2 {
@:Section2 Stuff
}
@Section ExtraSection {
@:Extra Section Stuff
}
我如何设置主视图,以便 Section1 和 Section2 各归其位,而其余的都以统一的方式处理? (例如像@RenderAllOtherSections())
@RenderSection("Section1")
@RenderSection("Section2")
@RenderBody()
@RenderAllOtherSections() // ExtraSection is rendered here. How?
更新:检查剃刀视图网页的基础对象后,我发现有一个字典,其中包含在前一个(调用)视图中定义的部分。它位于PreviousSectionWriters 参数中,但它有一个私有获取。还有,这个字典实际上是SectionWritersStack栈中的第二项,但是栈中也有一个私有的get。已经渲染的部分在 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) 是唯一可以使用的公共可访问的东西,但它并没有真正的帮助。
【问题讨论】:
-
我认为这不可能。
-
在这里找到了完美的解决方案:Asp.NET MVC Forums
标签: asp.net asp.net-mvc asp.net-mvc-3 c#-4.0 razor