【问题标题】:Razor use RenderSection when you don't know the name of it当您不知道它的名称时,Razor 使用 RenderSection
【发布时间】: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 asp.net-mvc asp.net-mvc-3 c#-4.0 razor


【解决方案1】:

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

如果我了解您的情况,您需要在 @RenderBody() 之后渲染部分 或者你可以有这样的东西:

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

在子视图中,您可以选择是否在“PostBodySection”中包含任何内容。

【讨论】:

  • 感谢您的回答,但我不能接受这是不可能的。我正在用我发现的一些东西更新问题
猜你喜欢
  • 2022-06-24
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多