Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 232 posts 902 karma points c-trib
    Jul 07, 2020 @ 18:31
    Martin Rud
    0

    How to include a string list in LINQ query?

    Hi,

    I am trying to create a generic site navigation partial view. It includes all levels recursively since that´s how I need it in the frontend.

    However some document types are not meant to be part of the navigation. I take care of that in this code:

    .Where(x => !(new[] { "doctype1", "doctype2" }).Contains(x.ContentType.Alias))
    

    But I would like to declare it outside the helper method. I have started this in this line:

    var excludeDocTypes = new[] { "doctype1", "doctype2" }; 
    

    But how do I "pull" the excludeDocTypes variable into the helper method? That´s out of my current competences scope! ;)

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{
        var excludeDocTypes = new[] { "doctype1", "doctype2" }; 
    }
    <nav>
        @RenderSubLevel(Model.Root())
    </nav>
    
    @helper RenderSubLevel(IPublishedContent parent)
    {
        if (parent !=  null)
        {
            <ul class="[email protected]">
                @foreach (var child in parent.Children(x => x.IsVisible()).Where(x => !(new[] { "doctype1", "doctype2" }).Contains(x.ContentType.Alias)))
                {
                    <li>
                        <a href="@child.Url" class="@(child.IsAncestorOrSelf(Model) ? "in-path" : null) @(child.Id == Model.Id ? "current" : null)">
                            @child.Url
                        </a>
                        @RenderSubLevel(child)
                    </li>
                }
            </ul>
        }
    }
    
  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Jul 07, 2020 @ 20:08
    Marc Goodson
    100

    Hi Martin

    The helper is 'self contained' but you can 'pass it in' if that makes sense:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{
        var excludeDocTypes = new[] { "doctype1", "doctype2" }; 
    }
    <nav>
        @RenderSubLevel(Model.Root(), excludeDocTypes)
    </nav>
    
    @helper RenderSubLevel(IPublishedContent parent, string[] excludeDocTypes)
    {
        if (parent !=  null)
        {
            <ul class="[email protected]">
                @foreach (var child in parent.Children(x => x.IsVisible()).Where(x => !excludeDocTypes.Contains(x.ContentType.Alias)))
                {
                    <li>
                        <a href="@child.Url" class="@(child.IsAncestorOrSelf(Model) ? "in-path" : null) @(child.Id == Model.Id ? "current" : null)">
                            @child.Url
                        </a>
                        @RenderSubLevel(child, excludeDocTypes)
                    </li>
                }
            </ul>
        }
    }
    

    There's quite a good example of these kinds of razor shenanigans in the Razor 'xml site map' tutorial here: https://our.umbraco.com/documentation/Tutorials/Creating-an-XML-Site-Map/

    regards

    Marc

  • Martin Rud 232 posts 902 karma points c-trib
    Jul 07, 2020 @ 21:38
    Martin Rud
    0

    So cool - thnx! :)

    It was the "string[]" part that I´d missed.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{
        var excludeDocTypes = new[] { "doctype1", "doctype2" }; 
    }
    <nav>
        @RenderSubLevel(Model.Root(), excludeDocTypes)
    </nav>
    
    @helper RenderSubLevel(IPublishedContent parent, string[] excludeDocTypes)
    {
        if (parent !=  null)
        {
            <ul class="[email protected]">
                @foreach (
                    var child in parent.Children(x => x.IsVisible()).
                    Where(x => !excludeDocTypes.Contains(x.ContentType.Alias))
                )
                {
                    <li>
                        <a href="@child.Url" class="@(child.IsAncestorOrSelf(Model) ? "in-path" : null) @(child.Id == Model.Id ? "current" : null)">
                            @child.Name
                        </a>
                        @RenderSubLevel(child, excludeDocTypes)
                    </li>
                }
            </ul>
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft