Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lars 66 posts 136 karma points
    Mar 23, 2015 @ 16:43
    Lars
    0

    Strongly typed Menu

    Hi. Umbraco has a nice menu snippet like this:


    @{ var selection = CurrentPage.Site().Children.Where("Visible"); }

    <ul>
        @foreach (var item in selection)
        {
            <li class="@(item.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
                <a href="@item.Url">@item.Name</a>
            </li>
        }
    </ul>

    Does anyone knows what this would be if I want it strongly typed, ie that I can use the intelegenser in Visual Studio to edit the code.
    /Lars

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 23, 2015 @ 17:22
    Dennis Aaen
    1

    Hi Lars,

    I think that you can do it like this.

    @{var selection = Model.Content.Site().Children.Where(x => x.IsVisible());}

    <ul>
       
    @foreach(var item in selection)
       
    {
           
    <li class="@(item.IsAncestorOrSelf(Model.Content) ? "current" : null)">
               
    <a href="@item.Url">@item.Name</a>
            </
    li>
       
    }
    </ul>

    Hope this helps,

    /Dennis

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 23, 2015 @ 17:53
    Jeroen Breuer
    0

    Hello,

    You could also build your menu in a controller and render that. That way it's strongly typed and no linq queries in your views. For example:

    Menu logic
    https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Extensions/BLL/ModelLogic.cs#L44

    Menu rendering:
    https://github.com/jbreuer/Hybrid-Framework-for-Umbraco-v7-Best-Practises/blob/master/Umbraco.Site/Views/Master.cshtml#L32 

    Have a look at the Hybrid Framework.

    Jeroen

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 23, 2015 @ 18:42
    Dennis Aaen
    100

    Hi Lars,

    I have just tested the code I posted earlier and I can see that is not possible yet to translate the Razor that comes fromfrom query builder,  to strongly typed

    But this should do the same,

    @{var selection = Model.Content.AncestorOrSelf(1).Children.Where(x => x.IsVisible());}

    <ul>
        @foreach(var item in selection)
        {
            <li class="@(item.IsAncestorOrSelf(Model.Content) ? "current" : null)">
                <a href="@item.Url">@item.Name</a>
            </li>
        }
    </ul>

    Hope this helps,

    /Dennis

  • mike 90 posts 258 karma points
    Mar 23, 2015 @ 23:28
    mike
    0

    I was doing it like Dennis' second example but then when I set up child doctypes on pages with the menu it started throwing an error.  I couldn't figure out why but using the "Visible" dynamic query feature instead of linq fixed it for some reason.  May have been some localised issue to my code but if you get it try doing like this:

    var selection =Model.Content.AncestorOrSelf(1).Children.Where("Visible");

     

     

  • Lars 66 posts 136 karma points
    Mar 24, 2015 @ 14:23
    Lars
    0

    Hi Both

    This was the solution. Now it is strongly typed. Thank you for your help :-)

    /Lars

     

    @{  IPublishedContent root = Model.Content.AncestorOrSelf(1); }

    <ul>
        <li class="@((Model.Content.Name == root.Name) ? "active" : null)"><a href="/">@root.Name</a></li>

        @foreach (IPublishedContent menuItem in root.Children.Where(x => x.IsVisible()))
        {
                else
                {
                    <li class="@((menuItem.Name == Model.Content.Name) ? "active" : null)">
                        <a href="@menuItem.Url">@menuItem.Name</a>
                    </li>
                }
            }
    }

    </ul>

Please Sign in or register to post replies

Write your reply to:

Draft