Copied to clipboard

Flag this post as spam?

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


  • Simon Kibsgård 62 posts 73 karma points
    Apr 01, 2011 @ 18:23
    Simon Kibsgård
    0

    Using dropdown values in Where statement with razor

    Hi

    I am trying to filter my DynamicNode selection with the text value of a dropdown on my nodes. It looks like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{var rootNode = Model.AncestorOrSelf(2);}
    @{var numberOfItems = 5; }
    @{var TwoLetterIso = "da"; }
    <ul>
        @foreach (var item in  @rootNode.Children.Where("languageISO == TwoLetterIso").Take(numberOfItems).OrderBy("UpdateDate")) {
            <li><a href="@item.Url">@item.Name</a> @item.languageISO, @TwoLetterIso</li>
        }
    </ul>

    ...but it fails to return anything. printing with @item.languageISO and @TwoLetterIso displays "da" just fine, but the where statement doesnt work. 

    Help is much appreciated 

    /Simon

  • Toni Becker 146 posts 425 karma points
    Apr 01, 2011 @ 19:04
    Toni Becker
    0

    i think you should use an if statement to check if the languageISO == "TwoLetterIso" to filter your output.
    You don't need to pull around every var the @{.. just use

    @{ var rootNode = Model.AncestorOrSelf(2);
    var numberOfItems = 5;
    var TwoLetterIso ="da";
    }

    @foreach (var item in rootNode.Children.Take(numberOfItems)...
    {
    if (languageISO == "TwoLetterIso")
    { give me my content }
    }

    Try this out but don't know if this will work. It's better to analyze the umbraco.config XML to see and realize the structure.

  • Simon Kibsgård 62 posts 73 karma points
    Apr 02, 2011 @ 08:58
    Simon Kibsgård
    0

    Toni, thank your for helping out.

    Your suggestion does work, but I won't do exactly what I need. I need five nodes, but I here I will only get the nodes passing my filter (languageIso == "da") within the first five nodes. Therefore probably less than five. I know I could do a counter, but I really love the syntax of .Where coming before .Take

  • Alex 78 posts 136 karma points
    Apr 02, 2011 @ 10:30
    Alex
    0

    @Simon Try this

    @foreach (var item in  @rootNode.Children.Where("languageISO == " + TwoLetterIso).Take(numberOfItems).OrderBy("UpdateDate"))

     

  • Simon Kibsgård 62 posts 73 karma points
    Apr 02, 2011 @ 11:16
    Simon Kibsgård
    0

    @Alex, thanks but that doesn't work either

    It filters everything away. I guess languageISO, being a dropdown value, is treated as something else in the .Where statement than when it's printed with @item.languageISO and renders the text value. I just wish I could step in and see what. Is there some way?

  • Toni Becker 146 posts 425 karma points
    Apr 02, 2011 @ 21:07
    Toni Becker
    0

    can you post your xml data output? you find it in the folder App_Data/umbraco.config

    Pleas post for the starting doctype holding your content.
    Example:

    <Doctype>
    <Doctype2>
    <Property1>
    <node id bla bla....>
    </Property1>
    </Doctype2>
    </Doctype>

  • Simon Kibsgård 62 posts 73 karma points
    Apr 03, 2011 @ 10:05
    Simon Kibsgård
    0

    Now I got it working!

    Looking at the XML as you suggested Toni got me thinking of changing the datatype.

    I changed the dropdown datatype from "Dropdown List" to "Dropdown List, publishing keys" and then tested for the numeric value like this

    .Where("languageISO == " + TwoLetterIsoNumericPublishKey)

    ...and that did the trick. Thanks for helping me solve it!

  • Toni Becker 146 posts 425 karma points
    Apr 03, 2011 @ 10:47
    Toni Becker
    0

    Okay no problem, happy that it helped you out. I think it's a good way to first analyze the xml structure when using razor. It helps me out solving some problems i had in the past.

Please Sign in or register to post replies

Write your reply to:

Draft