Copied to clipboard

Flag this post as spam?

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


  • Robin Nicholl 137 posts 277 karma points
    Feb 29, 2012 @ 01:32
    Robin Nicholl
    0

    Filtering DynamicNodeList Descendants

    I want to list only the descendant nodes whose "inList" property is true. This unfiltered example works…

    {@DynamicNodeList subnodes = @n.Descendants();}
    <div class="subnodes">
    @foreach (DynamicNode c in @subnodes) {
    <a href="@c.Url">@c.Name</a> } </div>

    … but I can't find a way to filter by "inList"…

    {@DynamicNodeList subnodes = @n.Descendants();}
    <div class="subnodes">
    @foreach (DynamicNode c in @subnodes) {
    if (c.GetProperty("inList") { <a href="@c.Url">@c.Name</a> } } </div>

    … doesn't work. I've tried using .Where in various places, but it either errors on save, or gives me "Error loading Razor… Object reference not set to an instance of an object".

    Can anyone help? I'm obviously doing something fundamentally wrong.

  • lucuma 261 posts 563 karma points
    Feb 29, 2012 @ 02:11
    lucuma
    0

    Have you tried:

    if (c.GetProperty("inList").Value) { ....}
  • Robin Nicholl 137 posts 277 karma points
    Feb 29, 2012 @ 02:30
    Robin Nicholl
    0

    Yes, that gives me an error on save: "Cannot implicitly convert type 'string' to 'bool'". If I try @c.GetProperty("inList").Value=="1" it saves OK, but gives me the error mentioned above when the page tries to render – "Object reference not set to an instance of an object" – which I don't understand.

  • Gareth Evans 143 posts 335 karma points c-trib
    Feb 29, 2012 @ 02:52
    Gareth Evans
    0

    Hi Robin

    That error suggests that one of the nodes in your list doesn't have anything set for inList
    If you want safe access of property values, you should use .GetPropertyValue("alias") which will return empty string when it doesn't exist.

    To filter the list of descendants, you could try this:

    @ForEach(var c in n.Descendants().Where("inList"))

  • Robin Nicholl 137 posts 277 karma points
    Feb 29, 2012 @ 03:07
    Robin Nicholl
    0

    Thanks, but this…

    @foreach (var in n.Descendants().Where("inList"){
       <div class="mininoodle"><href="@c.Url">@c.Name</a></div>
    }

    gives me this error: "The type arguments for method 'umbraco.MacroEngines.DynamicNodeList.Where(string, params object[])' cannot be inferred from the usage. Try specifying the type arguments explicitly."

    Not sure how to use .GetPropertyValue("alias") – this…

    if (c.GetPropertyValue("inList"){
       <div class="mininoodle"><href="@c.Url">@c.Name</a></div>
    }

    … gives me "Cannot implicitly convert type 'string' to 'bool'".

  • Gareth Evans 143 posts 335 karma points c-trib
    Feb 29, 2012 @ 03:09
    Gareth Evans
    0

    Can you try this:

     

    @foreach (var in n.Descendants().Where("inList == True"))

     

    Not sure that will work though.

    What data type is inList configured as?

    For get property value, it simply returns the property value as a string, so this is what you need to do:

    if (c.GetPropertyValue("inList") == "1") {
  • Robin Nicholl 137 posts 277 karma points
    Feb 29, 2012 @ 03:16
    Robin Nicholl
    0

    Ah, perfect! This works…

    @{DynamicNodeList subnodes @n.Descendants();}
       <div class="supernoodle">
       @foreach (DynamicNode in @subnodes{
          if (@c.GetPropertyValue("inList")=="1"{
             <div class="mininoodle"><href="@c.Url">@c.Name</a></div>
          }
       }
    </div>

    Thanks Gareth!

Please Sign in or register to post replies

Write your reply to:

Draft