2012-12-06 21 views

回答

3

確實寫了一個解決方案之前,任何人打我吧:)

@using EPiServer 
@using EPiServer.Core 
@using EPiServer.Filters 
@using EPiServer.Web.Mvc.Html 

<ul id="mainMenu"> 
     @{ 
      PageData startPage = ContentReference.StartPage.GetPage(); 
      PageDataCollection allChilds = DataFactory.Instance.GetChildren(((PageData)startPage).PageLink); 
      IEnumerable<PageData> filteredChilds = FilterForVisitor.Filter(allChilds).Where(p => p.IsVisibleOnSite() && p.VisibleInMenu); 
     } 

     @foreach (PageData item in filteredChilds) 
     { 
      <li> 
       <a href="/@item.URLSegment.ToString()"> 
        @item.Name 
       </a> 
      </li> 
     } 
</ul> 

凡GETPAGE是一個擴展方法

public static PageData GetPage(this PageReference pageLink) 
{ 
    return DataFactory.Instance.GetPage(pageLink); 
} 
+1

這可能是明智的裝配了IEnumerable在控制器或查看模型,而不是在實際的視圖中的代碼。 –

+0

我同意,但我已將代碼放入沒有任何控制器或默認模型的剃鬚刀母版頁中。也許我應該建造這個模型。 –

+0

另一種方法是爲您的主要導航和類似的東西做RenderAction(Joel Abrahamsson在EPi7開發課程上對此進行了演示)。 –