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.
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.
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"))
@foreach (var c in n.Descendants().Where("inList")) { <div class="mininoodle"><a 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"><a href="@c.Url">@c.Name</a></div> }
… gives me "Cannot implicitly convert type 'string' to 'bool'".
Filtering DynamicNodeList Descendants
I want to list only the descendant nodes whose "inList" property is true. This unfiltered example works…
… but I can't find a way to filter by "inList"…
… 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.
Have you tried:
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.
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"))
Thanks, but this…
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…
… gives me "Cannot implicitly convert type 'string' to 'bool'".
Can you try this:
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:
Ah, perfect! This works…
Thanks Gareth!
is working on a reply...