Copied to clipboard

Flag this post as spam?

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


  • Colm Garvey 40 posts 65 karma points
    Jan 29, 2017 @ 12:43
    Colm Garvey
    0

    Using "if item.propertyValue" to filter a selection

    Hi all.

    I have a selection generated from the children of the current page. Within that selection (let's call it Fruit) I have items and each item has a fruitType.

    This code doesn't work:

    @{
        var selection = CurrentPage.Children("fruit").Where("Visible");
    }
    <ul>
        @foreach(var item in selection){
            @if(@item.fruitType == "Apple");{
            <li>
                <a href="@item.Url">@item.Name</a><br/>
                @item.fruitName<br>
                @item.fruitType<br>
                @if (item.image != null && !(item.image is Umbraco.Core.Dynamics.DynamicNull))
                            { var m = Umbraco.Media(item.image);
                                <img src="@m.Url" alt="Picture of @item.Name" />
                            }
            </li>
            }
        }
    </ul>
    

    What I'm trying to do is to only list the items with a fruitType of "Apple". This value is selected from a dropdown list so I'm not sure if umbraco is storing this as the string "Apple" or as a numeric value.

    Any suggestions?

  • Colm Garvey 40 posts 65 karma points
    Jan 29, 2017 @ 16:44
    Colm Garvey
    0

    So... I checked what the option values were for the fruitType dropdown list and they're numeric, but substituting the numeric value made no difference.

    I've also tried several different permutations including just including this within the "forEach" routine:

    @if(item.fruitType != null)
                {**********}
    

    Still not working :(

    Starting to wonder whether or not this is something to do with the fact that I'm using Dropdown list to store the fruitType...

  • Colm Garvey 40 posts 65 karma points
    Jan 31, 2017 @ 12:18
    Colm Garvey
    0

    bump still not able to get this working... can anyone offer some insight on how to filter selection results based on a choice from a select list?

  • Colm Garvey 40 posts 65 karma points
    Feb 08, 2017 @ 11:13
    Colm Garvey
    0

    Still not working... I tried a few variations on a solution I found here:

    https://our.umbraco.org/forum/developers/razor/19087-Using-dropdown-values-in-Where-statement-with-razor

    but none work.

    It's odd that as soon as I add a simple @If conditional to the mix then it all goes pear shaped.

  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    Feb 08, 2017 @ 12:11
    Søren Kottal
    100

    Whats your error message?

    To me it seems like you have a few too many @'s

    @{
        var selection = CurrentPage.Children("fruit").Where("Visible");
    }
    <ul>
        @foreach(var item in selection){
            if (item.fruitType == "Apple") { /// I REMOVED THE @ AND ; HERE
            <li>
                <a href="@item.Url">@item.Name</a><br/>
                @item.fruitName<br>
                @item.fruitType<br>
                @if (item.image != null && !(item.image is Umbraco.Core.Dynamics.DynamicNull))
                            { var m = Umbraco.Media(item.image);
                                <img src="@m.Url" alt="Picture of @item.Name" />
                            }
            </li>
            }
        }
    </ul>
    
  • Alex Brown 129 posts 620 karma points
    Feb 08, 2017 @ 12:51
    Alex Brown
    0

    I agree with Søren. When you're using the @ helper to generate C# code on a view, you only need one "@" to begin the code branch.

    Søren's example should work as he starts the foreach (C# code) with an @ helper, then the following code on the next line down is an "if" statement, which is also C# - therefore you don't need to add another @ helper.

    If the li tag was above the if statement, then you would need another @ helper for the "if" statement - example below:

    <ul>
        @foreach(var item in selection){
            <li>
                @if (item.fruitType == "Apple") {
                     <a href="@item.Url">@item.Name</a><br/>
                     @item.fruitName<br>
                     @item.fruitType<br>
                     @if (item.image != null && !(item.image is Umbraco.Core.Dynamics.DynamicNull))
                            { var m = Umbraco.Media(item.image);
                                <img src="@m.Url" alt="Picture of @item.Name" />
                            }
            }
            </li>
        }
    </ul>
    
  • Colm Garvey 40 posts 65 karma points
    Feb 08, 2017 @ 15:36
    Colm Garvey
    0

    Thanks Søren, I'm a little confused by the lack of clear distinction between code and text with Razor. It's not like ASP, JSP or PHP where code blocks are clearly wrapped in specific tags.

    I just assumed that the @ symbol was alway required for functional blocks.

    Thanks also to Alex for clearing that misconception up.

    Just out of curiousity though, is there an "Umbraco Razor" Cookbook for common code use cases out there?

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 08, 2017 @ 15:47
  • Zubair Hasan 18 posts 88 karma points notactivated
    Nov 12, 2018 @ 17:16
    Zubair Hasan
    0

    I have a category document type which creates child node of books document type.

    I want to show on my web page every catname that includes all books that belongs to that category.

    Like cat1 --> Book1 Book2 Book3

    cat2 --> Book4 Book5 cat3 --> ......... so on....

Please Sign in or register to post replies

Write your reply to:

Draft