@{
    Model.Classes.Add("nav");
    
    // Model is Model.Menu from the layout. (Layout.Menu)
    var tag = Tag(Model, "ul");

    var items = (IList<dynamic>)Enumerable.Cast<dynamic>(Model.Items);

    if (items.Any()) 
    {
        items[0].Classes.Add("first");
        items[items.Count - 1].Classes.Add("last");
    }
}

<nav>
    @tag.StartElement
        @* See MenuItem shape template. *@
        @DisplayChildren(Model)
    @tag.EndElement
</nav>