Copied to clipboard

Flag this post as spam?

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


  • Peter van Geffen 54 posts 290 karma points
    Feb 21, 2022 @ 14:47
    Peter van Geffen
    0

    Creating a sub navigation list

    As mentioned in another topic, we are trying some existing code (V8) to copy into V9. Some of it doesn't work.

    Such like the subnavigation.

    What we had:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @using ContentModels = Umbraco.Web.PublishedModels;
    @{
        IEnumerable<IPublishedContent> children = Model.AncestorOrSelf(2).Children.Where(x => x.IsVisible()).Where(x=>x.ContentType.Alias !="nieuwsItem");
        if (children.Any()){
            @childPages(children);
        }
        @helper childPages(IEnumerable<IPublishedContent> pages){ 
            var naviLevel = pages.First().Level;
            <ul class="level-@(naviLevel)">
                @foreach (IPublishedContent page in pages){
                    <li>
                        <a href="@page.Url">@page.Name</a>
                        @if (page.Children.Where(x => x.IsVisible()).Any() && Array.IndexOf<string>(Model.Path.Split(','), page.Id.ToString()) > -1){
                            @childPages(page.Children.Where(x => x.IsVisible()));
                        }
                    </li>
                }
            </ul>
        }
    }
    

    And this is what i've found so far and tried:

    https://our.umbraco.com/forum/using-umbraco-and-getting-started/108001-recursive-menus#comment-335701

    Maybe i'm doing something wrong. If it works with some adjustments at the old situation it's also good (Maybe better for us)

  • Ambert van Unen 175 posts 819 karma points c-trib
    Feb 21, 2022 @ 15:37
    Ambert van Unen
    1

    It's easier if you post the errors you encounter ;-) If using a tool like Visual Studio it would immediately say @helper is not recognized as it no longer exists in v9.

    But you can do the following, change @helper to void and put it 'within code' and don't explicitly print it. Also 'page' is prohibited in a UmbracoViewPage (has a different meaning). If you do the following I reckon it should work:

        @{
            IEnumerable<IPublishedContent> children = Model.AncestorOrSelf(2).Children.Where(x => x.IsVisible()).Where(x => x.ContentType.Alias != "nieuwsItem");
            if (children.Any())
            {
                childPages(children);
            }
    
            void childPages(IEnumerable<IPublishedContent> pages)
            {
                var naviLevel = pages.First().Level;
                <ul class="level-@(naviLevel)">
                    @foreach (IPublishedContent item in pages)
                    {
                        <li>
                            <a href="@item.Url()">@item.Name</a>
                            @if (item.Children.Where(x => x.IsVisible()).Any() && Array.IndexOf<string>(Model.Path.Split(','), item.Id.ToString()) > -1)
                            {
                                childPages(item.Children.Where(x => x.IsVisible()));
                            }
                        </li>
                    }
                </ul>
            }
        }
    

    Only things I did is changing the helper to a normal method, and renaming 'page' to 'item'

  • Mark Drake 134 posts 458 karma points c-trib
    Feb 21, 2022 @ 15:59
    Mark Drake
    1

    To add to the explanation above, @helper is not part of .NET Core. This particular change is less about Umbraco and more about the underlying framework it's built on.

  • Peter van Geffen 54 posts 290 karma points
    Feb 22, 2022 @ 09:55
    Peter van Geffen
    0

    Thank you for your answer.

    I run some quick tests and do this in just the online environment (cloud). And it's not giving me any errors in return. Maybe it's better indeed to do this in Visual Studio.

    Allthough your adjustments doesn't seem to work either for me. So went with the option Dennis came up with.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 21, 2022 @ 18:30
    Dennis Aaen
    100

    Hi Peter,

    If you go to the backoffice and then settings --> partial views

    When you are there then you create a new partial

    Take the option from the snippet

    Chose the one called List Descendants From Current Page

    Then you can get an idea on how you can create the help method in v9

    Hope this helps you further

    /Dennis

  • Peter van Geffen 54 posts 290 karma points
    Feb 22, 2022 @ 09:58
    Peter van Geffen
    0

    Thank you again Dennis.

    I didn't knew this before (I'm a Umbraco noob). I'll keep this in mind for the furure.

    And again, it did the trick for me.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 22, 2022 @ 10:09
    Dennis Aaen
    0

    Hi Peter

    Great to hear it did the trick for you.

    Have a fantastic day

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft