2

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

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157

1 Answers1

4

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);
}
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • 1
    It's probably wiser to assemble the IEnumerable in a Controller or View Model instead of having the code in the actual View. – Johan Kronberg Dec 08 '12 at 09:52
  • I would agree but I have put the code in a razor masterpage that does not have any controller or default model. Maybe I should construct that model anyway. – Eric Herlitz Dec 09 '12 at 11:30
  • An alternative is to do RenderAction for your main navigation and similiar stuff (Joel Abrahamsson demo'ed this on the EPi7 Dev Course). – Johan Kronberg Dec 11 '12 at 09:30
  • That would of course do but I decided to follow this pattern http://world.episerver.com/Blogs/Eric-Herlitz/Dates/2012/12/Writing-a-multi-level-submenu-using-MVC-in-EPiServer-7/ – Eric Herlitz Dec 11 '12 at 09:34