Copied to clipboard

Flag this post as spam?

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


  • Bob 6 posts 56 karma points
    Feb 27, 2014 @ 20:13
    Bob
    0

    Check mulitple node levels for doc type and do common routine in Razor

    Hi All,

    I am wondering if it is possible to move thru a node and its children using razor and  perform a common routine, whether a specific task or validation (and whether this can be achieved using a helper) rather than having the same code repeating at each level.

    For example:

    Node1
    ------ Node 1-1
                    ------ Node 1-1-1
    ***************************************************

    For each (var c in @Model.Children)

           Dynamic Node1 =Model.NodeById(c.id);
           @foreach (var N1 in Node1.Children)

                       if(@N1.NodeTypeAlias = "DATA"
                       check N1.Data field 1
                       check N1.Data field 2 .....
                       display result ........

          Dynamic Node1-1 =Model.NodeById(Node1.id);
           @foreach (var N1-1 in Node1-1.Children)

                       if(@N1-1.NodeTypeAlias = "DATA"
                       check N1-1.Data field 1
                       check N1-1.Data field 2 .....
                       display result ........

         Dynamic Node1-1-1 =Model.NodeById(Node1-1.id);
           @foreach (var N1-1-1 in Node1-1-1.Children)
                       if(@N1-1-1.NodeTypeAlias = "DATA"
                       check N1-1-1.Data field 1
                       check N1-1-1.Data field 2 .....
                       display result ........


    So is it possible to wrap up the common part below for each level I go thru

      if(@N1.NodeTypeAlias = "DATA"
                       check N1.Data field 1
                       check N1.Data field 2 .....
                       display result ........

    Any thoughts would be appreciated.

    Cheers

     

     

     

     

     

     

  • Bob 6 posts 56 karma points
    Mar 04, 2014 @ 18:20
    Bob
    100

    Hi All,

    Does anyone have an idea or example on how to setup a reusable routine in razor?

    Cheers

  • Bob 6 posts 56 karma points
    Mar 07, 2014 @ 01:48
    Bob
    0

    Hi All,

    Figured it out, thought I'd post my solution in case its helpful

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    ------ define helper which has common routine, note using dynamic so I can get property values from item -----------------

    @helper ValidationRoutine(dynamic item) {
        if(@item.NodeTypeAlias=="somedoctype"){
         ----- do validation
          }
     }

    ------- create process to loop though all nodes in parent for x number of levels

    @foreach(var c in @Model.Children)
    {
    dynamic _Folder = Model.NodeById(c.Id);
                @foreach (var itemL1 in _Folder.Children)
                {
                    @ValidationRoutine(@itemL1)      ----- this part calls the helper

                    {   
                        dynamic _FolderL2 = Library.NodeById(@itemL1.Id);
                            @foreach (var itemL2 in _FolderL2.Children)
                            {  
                                @ValidationRoutine(@itemL2)   ----- this part calls the helper

    .... etc ...etc

Please Sign in or register to post replies

Write your reply to:

Draft