Copied to clipboard

Flag this post as spam?

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


  • Erik 7 posts 37 karma points
    May 04, 2014 @ 13:23
    Erik
    0

    Passing an umbraco Node as a parameter to a helper function

    Hi,

    I'm new to Razor and was wondering how I can pass an umbraco node to a helper function. Declaring the parameter as a Node does not work. I've got the following sample listed below.

     

    @using umbraco.MacroEngines;
    @helper PrintBlock(DynamicNode node)
    {
        if (node!= null)
        {
            <div class="pnl">
                if (!String.IsNullOrEmpty(node.title))
                {
                    <h2>@node.title</h2>   
                }
                <p>@node.content</p>
                if (!String.IsNullOrEmpty(node.referral))
                {
                    var referral= Model.GetNodeById(node.referral);
                    if (referral != null)
                    {
                        <a href="@referral.Url" class="btn">@node.referralText</a>   
                    }
                }
            </div>
        }
    }

    @PrintBlock(Model.GetNodeById(Model.info1));
    @PrintBlock(Model.GetNodeById(Model.info2));
    @PrintBlock(Model.GetNodeById(Model.info3));

    Info1, 2, and 3 are ContentPickers on the current node. Same with referral, this is a ContentPicker on the node referred to bij info1 (or 2 or 3).

    I get the exception that 'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'title' and no extension method 'title' accepting a first argument of type 'umbraco.MacroEngines.DynamicNode' could be found (are you missing a using directive or an assembly reference?). How can I pass the resulting node from Model.GetNodeById to a helper function?

    Any help appreciated.

    Regards,

    Erik.

  • Jon Dunfee 199 posts 468 karma points
    May 07, 2014 @ 06:26
    Jon Dunfee
    0

    I'm still learning myself.  I, basically, moved away from dynamic properties in favor of method GetPropertyValue. (e.g. node.GetPropertyValue("referralText")).  Instead of Model.GetNodeById, you could go Umbraco.TypedContent(Model.GetPropertyValue("info1")) then you are passing a typed object to the helper (have to update the helper, of course).

  • Nawaz 21 posts 74 karma points
    Aug 26, 2015 @ 15:24
    Nawaz
    0

    Here is some code that works for me

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
      var myNode = Model.NodeById(1219);
    
      foreach (var item in myNode.Children) {   
                @showProduct(item);
          }
      }                                                             
    }
    
    @helper showProduct(dynamic prod){
        <div>
            @prod.productName
        </div>
    
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies