Copied to clipboard

Flag this post as spam?

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


  • mizzle 90 posts 325 karma points
    May 24, 2019 @ 20:46
    mizzle
    0

    Get and store values of a multi-node tree picker into a list for filtering?

    I'm trying to filter a list of displayed blog pages by categories, which are chosen via a multi-node tree picker in Umbraco on each blog page, but I'm unable to retrieve and store the values from the multi-node tree picker into a list that can be compared to the selected category.

    Any attempt to get and store the values results in the content rendering as something along the lines of

    System.Collections.Generic.List`1[Umbraco.Core.Models.IPublishedContent]

    The only documentation for the multi-node tree picker is the following:

        var typedMultiNodeTreePicker = Model.Value<IEnumerable<IPublishedContent>>("featuredArticles");
    foreach (var item in typedMultiNodeTreePicker)
    {
        <p>@item.Name</p>
    }
    

    which is fine for displaying content, but I can't use it to store content for later use. I've tried creating an empty list of strings and foreach looping @item.Name into them, but I still get "System" results instead of actual results. Same with attempting to store nodes into a list, integers (node IDs) into a list, etc.

    I'm not familiar enough with MVC to know what I might be doing wrong.

    Using Umbraco version 7.14.0.

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    May 29, 2019 @ 15:56
    Dan Diplo
    0

    Say your filter category is an IPublishedContent variable called filterCategory then you should be able to compare it to see if it's in the list of IDs for the property on your blog post that stores the categories.

    I don't know what your models or doc types look like so you'll need to adjust this, but you can do something like this... (it assumes each blog post has a categories property that is the MNTP that returns a list of the selected categories):

    IEnumerable<IPublishedContent> blogPosts; // your list of all blog posts you have populated
    
    IPublishedContent filterCategory; // the category you want to filter the list by
    
    var filteredPosts = blogPosts.Where(p => p.GetPropertyValue<IEnumerable<IPublishedContent>>("categories").Select(c => c.Id).Contains(filterCategory.Id));
    

    Basically what this does is check if the filterCategory ID is contained within the list of Ids selected for the blog post.

    Hope that makes sense!

Please Sign in or register to post replies

Write your reply to:

Draft