Search code examples
asp.net-mvc-4episerverepiserver-7

Building a top-menu in EPiServer 7 using MVC and Razor


I'm trying to figure out how to implement a menu in EPiServer 7. The only documentation available shows how to do this with webforms which I'm not using

Any advice how to do this?

thanks


Solution

  • Did write a solution before anyone beat me to it :)

    @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>
    

    Where GetPage is an extension method

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