Copied to clipboard

Flag this post as spam?

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


  • Jamie Pollock 27 posts 102 karma points c-trib
    Mar 23, 2011 @ 12:16
    Jamie Pollock
    2

    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).

    1. I'm creating a DynamicNodeList from a comma delimitered property - No problems here (perhaps a little clunky but ehh)
    2. Checking the DynamicNodes coming in to ensure they're valid - again not a problem here with GetProperty.
    3. 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?)

    Well anyways, here's my macro code:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;
    @using System.Linq;

    @{
    //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 (dn != null && (dn.HasProperty("title") && dn.GetProperty("title").Value.Length > 0) && (dn.HasProperty("image") && dn.Image != 0))
    {
    //Everything cushdy? alright add it.
    dnl.Add(dn);
    }
    }
    }

    @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.

    Jamie

     

  • Alex 78 posts 136 karma points
    Mar 23, 2011 @ 14:24
    Alex
    2

    To fix your "Image" property problem try changing

    DynamicNode dn = new DynamicNode(id);

    to

    dynamic dn = new DynamicNode(id);

  • Alex 78 posts 136 karma points
    Mar 23, 2011 @ 14:27
    Alex
    2

    Also, I think this should give you a DynamicNodeList, but I haven't tested it

     

    dynamic dnl = Model.NodeById(Model.boxes.Split(','));
  • Jamie Pollock 27 posts 102 karma points c-trib
    Mar 23, 2011 @ 14:59
    Jamie Pollock
    0

    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

  • Alex 78 posts 136 karma points
    Mar 23, 2011 @ 17:59
    Alex
    0

    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!

     

Please Sign in or register to post replies

Write your reply to:

Draft