2011-02-04 90 views
6

我有一個Page.cshtml類似於以下(不工作):有沒有辦法使用asp.net mvc Razor ViewEngine製作@section選項?

@{ 
    Layout = "../Shared/Layouts/_Layout.cshtml"; 
    var mycollection = (ViewBag.TheCollection as IQueryable<MyCollectionType>); 
} 

<h2>@ViewBag.Title</h2> 

content here 

@if (mycollection != null && mycollection.Count() > 0) 
{  
    @section ContentRight 
    {  
     <h2> 
      Stuff 
     </h2> 
     <ul class="stuff"> 
      @foreach (MyCollectionType item in mycollection) 
      { 
       <li class="stuff-item">@item.Name</li> 
      } 
     </ul> 
    } 
} 

正如我所說的,這是行不通的。如果集合中沒有任何內容,我不想定義該部分。有什麼辦法可以做這種工作嗎?如果不是,我的其他選擇是什麼?我對這款Razor ViewEngine很陌生。

編輯

在我的佈局,我有:

@if(IsSectionDefined("ContentRight")) 
{ 
    <div class="right"> 
     RenderSection("ContentRight") 
    </div> 
} 

什麼,我不希望是div輸出時部分是空的。

回答

3

我最後做的東西有點哈克讓它工作,我需要它。

我的網頁上,我有:

@{ 
    Layout = "../Shared/Layouts/_Layout.cshtml"; 
    var mycollection = (ViewBag.TheCollection as IQueryable<MyCollectionType>); 
    ViewBag.ShowContentRight = mycollection != null && mycollection.Count() > 0; 
} 

然後在我的佈局,我有:

@if(IsSectionDefined("ContentRight") && (ViewBag.ShowContentRight == null ||ViewBag.ShowContentRight == true)) 
{ 
    <div class="right"> 
     RenderSection("ContentRight") 
    </div> 
} 
else if(IsSectionDefined("ContentRight")) 
{ 
    RenderSection("ContentRight") 
} 

如果部分被定義它必須被渲染,但如果沒有內容,我不想<div> s

如果有更好的方法我想知道。

-1

您可以IsSectionDefined

Layout.cshtml包住整個一節中的if語句:

@if (IsSectionDefined("ContentRight")) 
{ 
    <div> 
    @RenderSection(name: "ContentRight", required: false) 
    </div> 
} 

你CSHTML頁:

@section ContentRight 
{  
    @if (mycollection != null && mycollection.Count() > 0) 
    { 
    <h2> 
     Stuff 
    </h2> 
    <ul class="stuff"> 
     @foreach (MyCollectionType item in mycollection) 
     { 
      <li class="stuff-item">@item.Name</li> 
     } 
    </ul> 
    } 
} 
+0

你想要把IsSectionDefined在佈局文件,並調用rendersection與所需=假 – ajma 2011-02-04 21:58:03

+0

這就是我有現在,但它的輸出div,因爲該部分即使沒有任何內容也被定義。 – 2011-02-04 21:58:43

2

渲染器期望在佈局文件中某個時候調用該方法。你可以欺騙渲染器並使用「全局」條件(認爲登錄)。

@{ 
    ViewBag.content = RenderBody(); 
} 
@if (Request.IsAuthenticated) { 
     @ViewBag.content; 
} 
else { 
     @Html.Partial("_LoginPartial") 
} 
0

擴展方法與私人靜態只讀字段信息的PERF:

private static readonly FieldInfo RenderedSectionsFieldInfo = typeof(WebPageBase).GetField("_renderedSections", BindingFlags.Instance | BindingFlags.NonPublic); 

public static void EnsureSectionsAreRegisteredAsRendered(this WebPageBase webPageBase, params string[] sectionNames) 
{ 
    var renderedSections = RenderedSectionsFieldInfo.GetValue(webPageBase) as HashSet<string>; 
    if (renderedSections == null) 
    { 
     throw new WebCoreException("Could not get hashset from private field _renderedSections from WebPageBase");  
    } 
    foreach (var sectionName in sectionNames) 
    { 
     if (!renderedSections.Contains(sectionName)) 
     { 
      renderedSections.Add(sectionName); 
     } 
    } 
} 

在你CSHTML:

@{ this.EnsureSectionsAreRegisteredAsRendered("SectionName1", " SectionName2", "…"); } 

是的,是的,是的....我知道...壞的反思!使用您自己的風險:)

0

我用在我看來基類下面的方法(從這個優秀的博客文章http://haacked.com/archive/2011/03/05/defining-default-content-for-a-razor-layout-section.aspx/):

public HelperResult RenderSection(string name, Func<dynamic, HelperResult> defaultContents) 
{ 
    if (IsSectionDefined(name)) 
    { 
     return RenderSection(name); 
    } 
    return defaultContents(null); 
} 

如果你沒有觀點的基類,我建議因爲它可以讓你爲視圖添加各種小的額外功能。只要創建一個類具有以下特徵:public abstract class MyViewPage<T> : WebViewPage<T>然後將其設置在web.config

<system.web.webPages.razor> 
    <pages pageBaseType="MyViewPage"> 
    ... 
    </pages> 
</system.web.webPages.razor> 
相關問題