v4.7: DynamicNode.GetProperty("propertyAlias") vs. DynamicNode.propertyAlias
Hey folks, I'm an old hand at XSLT and my linq skills are lacking too due to being stuck in .net 2.0 until fairly recently. I have been trying to get into the Razor syntax as best as I can. Here's my problem step-by-step (the code follows after).
I'm creating a DynamicNodeList from a comma delimitered property - No problems here (perhaps a little clunky but ehh)
Checking the DynamicNodes coming in to ensure they're valid - again not a problem here with GetProperty.
However upon trying to use a child alias property ie dn.image rather than dn.GetProperty("image") I get the following error:
error CS1061: 'umbraco.MacroEngines.DynamicNode' does not contain a
definition for 'Image' and no extension method 'Image' accepting a first
argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are
you missing a using directive or an assembly reference?)
@{ //Create DynamicNodeList much like a node-set in XSLT DynamicNodeList dnl = new DynamicNodeList();
//Split currentPage boxes property to get the box Node IDs foreach(string id in Model.boxes.Split(',')) { //Create a DynamicNode based on the node ID found DynamicNode dn = new DynamicNode(id);
/*Do some validation to ensure the node has the property required: * 1. is the node not null * 2. Does the node has a title property & more importantly does it have a value? * a. should this be replaced with dn.Title? * 3. Do the same with the "image" property */
@if (dnl.Items.Count > 0) { //This will output each valid node in an unordered list eventually. @dnl.Items.Count; }
I have a few questions regards this. Is there a better way to make a DynamicNodeList using Linq. But more importantly where's my understanding gone wrong so that dn.Title doesn't work? (please note: in both cases title & image are lower case as property alias.
I want to get to know Razor and I've been scan reading blog posts for the past few hours.
Hi Alex, What can I say? You're a star! :) I'd switched over to using dynamic before lunch and found that worked. I'm not sure which I prefer DynamicNode or Dynamic. But I can see it working fine.
Also the second solution does work. I'm applying it to a foreach still so I can use an if to validate the nodes coming through.
Small steps, I'll probably get some linq in there eventually but right now I like to know I have X valid nodes before commiting them to containing HTML element.
My understanding (which may be wrong) is that if you cast to DynamicNode then it will try to strongly-type against the property names at compile time and throw the error you got, where as if you declare it as dynamic then it ignores the members at compile time and then uses the "magic" methods at runtime to find the properties. Not sure if that is 100% correct but it seems to point me in the right direction!
v4.7: DynamicNode.GetProperty("propertyAlias") vs. DynamicNode.propertyAlias
Hey folks,
I'm an old hand at XSLT and my linq skills are lacking too due to being stuck in .net 2.0 until fairly recently. I have been trying to get into the Razor syntax as best as I can. Here's my problem step-by-step (the code follows after).
error CS1061: 'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'Image' and no extension method 'Image' accepting a first argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are you missing a using directive or an assembly reference?)
Well anyways, here's my macro code:
I have a few questions regards this. Is there a better way to make a DynamicNodeList using Linq. But more importantly where's my understanding gone wrong so that dn.Title doesn't work? (please note: in both cases title & image are lower case as property alias.
I want to get to know Razor and I've been scan reading blog posts for the past few hours.
Jamie
To fix your "Image" property problem try changing
to
Also, I think this should give you a DynamicNodeList, but I haven't tested it
Hi Alex,
What can I say? You're a star! :) I'd switched over to using dynamic before lunch and found that worked. I'm not sure which I prefer DynamicNode or Dynamic. But I can see it working fine.
Also the second solution does work. I'm applying it to a foreach still so I can use an if to validate the nodes coming through.
Small steps, I'll probably get some linq in there eventually but right now I like to know I have X valid nodes before commiting them to containing HTML element.
Thanks again,
Jamie
No problem.
My understanding (which may be wrong) is that if you cast to DynamicNode then it will try to strongly-type against the property names at compile time and throw the error you got, where as if you declare it as dynamic then it ignores the members at compile time and then uses the "magic" methods at runtime to find the properties. Not sure if that is 100% correct but it seems to point me in the right direction!
is working on a reply...