1

我有兴趣根据页面级别而不是模板级别在不同位置呈现部分,这可能吗?例如:

模板:

<body>
@if (IsSectionDefined("oneColumn")) {
  @RenderSection("oneColumn", false)    
  <div class="row">             
     @RenderSection("oneColumn_1", true)
  </div>
}

@if (IsSectionDefined("twoColumn")) {
@RenderSection("twoColumn", false)  
  <div class="row">             
    @RenderSection("twoColumn_1", true)
    @RenderSection("twoColumn_2", true)
  </div>
}
</body>

页面级别:

@{
  Layout = "template.cshtml";
}

@section twoColumn{}    
@section oneColumn{}

@section twoColumn_1 { <div>THIS IS COLUMN 1 - ColSpan 1</div> }
@section twoColumn_2 { <div>THIS IS COLUMN 2 - ColSpan 2</div> }
@section oneColumn_1 { <div>THIS IS COLUMN 1</div> }

这总是将 oneColumn 部分呈现在 twoColumn 部分之上。但我想要 oneColumn 之上的 twoColumn。希望在不创建其他模板的情况下做到这一点。

4

1 回答 1

0

您无法指示将在“页面级别”中呈现部分的位置,除非您引入另一个级别的子模板/部分视图。

我假设您不希望总是在一列内容之上显示两列内容。

如果您希望能够偶尔在一个列部分上方呈现两列,则将另一个部分添加到您的模板(布局视图)中,例如:

@if (IsSectionDefined("oneColumnAfterTwo")) {
@RenderSection("oneColumnAfterTwo", false)  
  <div class="row">             
    @RenderSection("oneColumn_1", true)
  </div>
}

并修改您的页面/视图以包含oneColumnAfterTwo而不是oneColumn在需要的地方。

于 2013-06-26T18:19:27.567 回答