Copied to clipboard

Flag this post as spam?

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


  • Mygel 9 posts 40 karma points
    Dec 06, 2014 @ 13:23
    Mygel
    0

    How to merge two NodebyID into one variable?

    I am using Umbraco and I would like to add two variables together that will display articles in both children.

    var nodes = Model.NodeById(1195).Children();
    var nodes2 = Model.NodeById(1201).Children();
    
    var test = Model.NodesById(nodes, nodes2);

    It's not working and throwing an error. Is there another way to do this? I found this on the forum board, but It doesn't seem to work for me.

    link: http://our.umbraco.org/forum/developers/razor/47078-how-to-merger-DynamicNode?p=0#comment168589

  • jivan thapa 194 posts 681 karma points
    Dec 06, 2014 @ 19:11
    jivan thapa
    0

    Have you try

    var test  = nodes.Concat(nodes2);
    

    what version of umbraco are you using?

  • Mygel 9 posts 40 karma points
    Dec 06, 2014 @ 19:30
    Mygel
    0

    I am using Umbraco 7.1.7 with MVC.

    I think that NodebyID is for the older versions of Umbraco, but i'd like to get my website up and running. This is the last thing I need before I think the functionality of my website is complete. I will go back and optimize it later.

    var nodes = Model.NodeById(1195).Children();

    This will display all children nodes under a certain folder. 
    -Beginner
    -Article1 
    -Article2
    -Article3

    and 

    var nodes2 = Model.NodeById(1201).Children();
    This will do the same thing.

    -Intermediate 
    -Article1 
    -Article2
    -Article3 

    I would like to display all children from both cateigories.
    NodesbyID will just show be
    Intermediate and Beginner folders but not actually the children.

  • jivan thapa 194 posts 681 karma points
    Dec 06, 2014 @ 19:52
    jivan thapa
    0

    This may work, I have not tested.

    var nodes1 = Umbraco.TypedContent(1195).Children;
        var nodes2 = Umbraco.TypedContent(1201).Children;
    
        var nodes = nodes1.Concat(nodes2);
    

    or

    // or if there is only child items under 1195, and 1201 nodes

      var node = Umbraco.TypedContent(new[] {1195, 1201})
            .DescendantsOrSelf<IPublishedContent>().Where(y => y.Id != 1195 || y.Id != 1201);
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 06, 2014 @ 20:20
    Dennis Aaen
    0

    Hi Mygel,

    Here is a way where you get the children of 2 nodes,

    @{ 

    var nodes1 = Umbraco.Content(1195).Children.Where("Visible");
    var nodes2 = Umbraco.Content(1201).Children.Where("Visible");

    var nodesList = nodes1.Concat(nodes2).ToList();
        foreach(var page in nodesList){
              <li class="@(page.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
       
    }

    Hope this helps,

    /Dennis

  • Mygel 9 posts 40 karma points
    Dec 06, 2014 @ 20:21
    Mygel
    0

    Thank you Jivan for your response. Unfortunately that is not working because I am using a Razor macro rather than MVC. I'm still trying to wrap my head around all of this, sorry!

    Here is my work so far if this helps anyone
    http://pastebin.com/jdFrUXJu ;

     

  • jivan thapa 194 posts 681 karma points
    Dec 06, 2014 @ 20:45
    jivan thapa
    0

    Hi, Mygel,

    Is Umbraco.TypedContent() not working? 
    

    You need to inherit the view page from

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 06, 2014 @ 20:49
    Dennis Aaen
    100

    Hi Mygel,

    If you are using the old DynamicNode razor, you can do it like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @*
        Model = The current page the macro is executed on
                @Model.bodyText

        Parameter = collection of parameter values passed from the macro
                @Paramter.myParam

        Library = utillity library with common methods
                @Library.NodeById(1233)
    *@

    @* The fun starts here *@


    @{
        var nodes1 = Library.NodeById(1195).Children;
        var nodes2 = Library.NodeById(1201).Children;
       
        var nodesList = nodes1.Concat(nodes2).ToList();
       
        foreach(var page in nodesList){
              <li class="@(page.IsAncestorOrSelf(Model) ? "current" : null)">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    }

    I think that you should consider one day to have a look at the Partial view or Partial view macros, because the DynamicNode razor is deprecated. And it´s is not so hard to convert the DynamicNode razor to Dynamic razor or strongly typed razor. To get an idea on how to convert the DynamicNode to Dynamic Razor see this documentation in the bottom of the page. http://our.umbraco.org/documentation/Reference/Templating/Macros/Partial-View-Macros/

    Hope this helps,

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft