当你不知道它的名字时,Razor会使用RenderSection

时间:2012-06-15 14:10:49

标签: asp.net asp.net-mvc asp.net-mvc-3 c#-4.0 razor

假设子视图:

@Section Section1 {
   @:Section1 Stuff
}

@Section Section2 {
   @:Section2 Stuff
}

@Section ExtraSection {
   @:Extra Section Stuff
}

如何设置主视图以便第1节和第2节转到它们的位置,所有其余部分以统一的方式处理? (例如像@RenderAllOtherSections())

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

更新:在检查了剃刀视图网页的基础对象后,我发现有一个字典,其中包含在上一个(调用)视图中定义的部分。它位于PreviousSectionWriters参数中,但这有一个私有的get。此外,这个字典实际上是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)是唯一可以使用的公共访问方法,但它并没有真正帮助。

1 个答案:

答案 0 :(得分:1)

这是不可能的。 如果这样的事情是可能的,那么母版页将如何理解哪个部分将是第一个,第二个......依此类推。?

如果我理解你的情况,你需要在@RenderBody()之后呈现部分 Alternativle你可以有这样的东西:

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

在儿童视图中,你可以在&#34; PostBodySection&#34;中选择任何东西。或不。

相关问题