Copied to clipboard

Flag this post as spam?

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


  • Meni 247 posts 483 karma points
    Feb 05, 2023 @ 07:58
    Meni
    0

    How to filter a node by Checkbox List in Umbraco 11

    Hi,

    In my Umbraco 7 website I have nodes which have few categories.

    Every time I add a new item to a node with mix categories, I select the category of this item. Then, when I want to display the magazine archive, I filter the node by the specific category, so I only display the items from a specific category.

    In my Umbraco 7 the code looks as follow:

        // ************* Mixed Nodes *****************
    if (OpliCategories == "Photonics")
    {
        magazine_pagesToList = magazine_node.DescendantsOrSelf().Where("OpliCategory == @0", "Photonics");
    }
    
    if (OpliCategories == "Lasers")
    {
        magazine_pagesToList = magazine_node.DescendantsOrSelf().Where("opliCategory == @0", "Lasers");
    }
    

    etc.

    In my Umbraco 11 I tried to do the following, but I'm getting an empty node:

    // ************* Mixed Nodes *****************
    if (OpliCategories == "Photonics")
    {
        magazine_pagesToList = magazine_node.DescendantsOrSelf().Where(n => n.Value("OpliCategory") == "Photonics");
    }
    
    if (OpliCategories == "Lasers")
    {
        magazine_pagesToList = magazine_node.DescendantsOrSelf().Where(n => n.Value("OpliCategory") == "Lasers");
    }
    

    Why I'm not getting the news items that matching the checkbox for each news item?

    Thanks

  • Dhanesh Kumar MJ 158 posts 511 karma points c-trib
    Feb 05, 2023 @ 12:10
    Dhanesh Kumar MJ
    0

    Hi Meni,

    n.Value("OpliCategory") == "Lasers") the n.Value is return the type object , and here you match with a value that is string Lasers!

    Please cast the value to string and then try to check the conditions.

    n.Value("OpliCategory")?.ToString()== "yourvalue")

    But I'm suggesting to use the models builder, which is quite easy to configure, and it will generate strongly typed models, which will give intellisense in code editor tools(vscode,visual studios.. etc) and give a better understanding about the return type.

    Ref: Models Builder

    Dhanesh

  • Meni 247 posts 483 karma points
    Feb 06, 2023 @ 07:07
    Meni
    100

    Hi, thanks.

    n.Value("OpliCategory")?.ToString()== "yourvalue")
    

    Didn't work. It seems it returns an array.

    So what I did is the following:

    n.Value<IEnumerable<string>>("OpliCategory")?.First()== "yourvalue")
    

    and now it's working.

    Thanks anyway for your help!

Please Sign in or register to post replies

Write your reply to:

Draft