Copied to clipboard

Flag this post as spam?

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


  • David Hammer 41 posts 67 karma points
    Oct 14, 2013 @ 12:28
    David Hammer
    0

    Need to get Children from Model

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using System.Xml.XPath;
    @using System.Xml;
    @{ 
      var panelBoxes = Model.DescendantsOrSelf("PanelBoxes").Where("Visible");
    
      foreach (var page in panelBoxes.Children.Where("Visible").Any())
      {
        if (page.NodeTypeAlias = "PanelTextBox")
        {
          <a href="@page.Url">@page.Name</a>
        }
    

    Hello guys. I'm trying to create a right panel, containing different PanelBoxes, therefore I've created a macro that runs through the model and its descendants, and from there I want to get the children of that model, and if it's node type equals a PanelTextBox or a PanelImageBox or a PanelTextAndImageBox I want it to do different stuff. Now here is my problem. Right now the loading of the macro breaks because of line 7.

      foreach (var page in panelBoxes.Children.Where("Visible").Any())
    

    This is the error:

    Error Loading Razor Script (file: Panel Boxes) 'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'Children'

    What else can I do? And if you guys know, please tell me what my problem is! Thank you upfront.

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 14, 2013 @ 12:40
    Fuji Kusaka
    0

    Hi David,

    Try this instead

    List<DynamicNode> panelBoxes = @Model.AncestorOrSelf(2).Descendants("PanelBoxes").Items;
    foreach(var page in panelBoxes.Where(x=>x.GetProperty("umbracoNaviHide").Value !="1")){
    <a hrref="@page.Url">@page.Name</a>
    }
  • David Hammer 41 posts 67 karma points
    Oct 14, 2013 @ 13:52
    David Hammer
    0

    Hi Fuji, and thanks for the quick response..

    I'm sorry to say that I have absolutely no idea what you want me to do XD

    List<DynamicNode> panelBoxes = @Model.AncestorOrSelf(2).Descendants("PanelBoxes").Items;
    

    Somehow the List gives me an error, and DynamicNode isn't supported. The generic type list T requires 1 type argument and the whole thing won't run.

    .

    .Where(x=>x.GetProperty("umbracoNaviHide").Value !="1")
    

    Where the what what equals what in what?

    This is my code right now:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using System.Xml.XPath;
    @using System.Xml;
    
    @{   
    List<DynamicNode> panelBoxes = @Model.AncestorOrSelf(2).Descendants("PanelBoxes").Items;
    
    foreach (var page in panelBoxes.Where(x=>x.GetProperty("umbracoNaviHide").Value !="1"))
    {
    if (page.NodeTypeAlias = "PanelTextBox")
    {
      <a href="@page.Url">@page.Name</a>
    }
    
    if (page.NodeTypeAlias = "PanelRssBox")
    {
      XmlTextReader rssReader = new XmlTextReader(page.panelRssBoxUrl);
      XmlDocument doc = new XmlDocument();
    
      doc.Load(rssReader);
    
      XmlNodeList rssItems = doc.SelectNodes("//item");
    
      var maxItems = int.Parse(page.panelRssBoxNoFeeds);
      var displayCount = rssItems.Count < maxItems ? rssItems.Count : maxItems;
    
      <h4>@page.panelRssBoxTitle</h4>   
      for (int index = 0; index < displayCount; index++)
      {
        var node = rssItems[index];
    
        <a href="@node["link"].InnerText">@node["title"].InnerText</a>
        @Convert.ToDateTime(node["pubDate"].InnerText).ToString("dddd d MMMM yyyy")    
      }
    }
    
    if (page.NodeTypeAlias = "PanelImageBox")
    {
      <a href="@page.Url">@page.Name</a>
    }
    }
    }
    

    This breaks pretty fast, and please dont mind the indention mixup..

    Do you have any other suggestions?

    Kind Regards

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 14, 2013 @ 14:10
    Fuji Kusaka
    0

    Hi David,

    You dont need to make another if to test for the DocType again since your are already listing all docType "PanelBoxes" using Descendants.

    Try this and make assuming you are on level 2 here AncestorOrSelf(2).


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

    @{  
    List<DynamicNode> panelBoxes =@Model.AncestorOrSelf(2).Descendants("PanelBoxes").Items;

    foreach(var page in panelBoxes.Where(x=>x.GetProperty("umbracoNaviHide").Value!="1"))
    {

    }
    }
  • David Hammer 41 posts 67 karma points
    Oct 14, 2013 @ 14:32
    David Hammer
    0

    Hello again, and thank you for your time.

    As I see it (correct me if I'm wrong)

    I'm creating this macro to list all of my boxes in a sidebar. I have 3 different types of boxes, therefore I need to create if-statements to divvi up the markup of every type of box. So I don't quite understand the "I don't need the other if-statements" :D

    Also the problem I have currently isn't really weather or not it works, just that

    List<DynamicNode>
    

    Gives me the validation errors:

    List : Using the generic type 'System.Collections.Generic.List

    DynamicNode : Validation (HTML5): Element 'dynamicnode' is not supported.

    I'm pretty sure I'm doing something wrong, but I am clueless.

    Again thank you so much for your help.

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 14, 2013 @ 16:15
    Fuji Kusaka
    0

    Hi David,

    Sorry, i didnt pay attention to the last part of your code there. So basically you want to display all Nodes present and from there check which docType is being used right ?

    Can you please show us your content structure ?

  • David Hammer 41 posts 67 karma points
    Oct 14, 2013 @ 16:24
    David Hammer
    0

    Sure, here it is

    • Content
      • Frontpage
        • Articles
        • Magazines
        • Subscription
        • Debate
        • About
        • Panel Boxes
          • Image Box
          • Text Box
          • Image and Text Box

    Pay no attention to the Frontpage placement, it is essentially done for an easy way to get rid of the 'frontpage' navigation heading.

    What i basically want is to get all 'Panel Boxes' children nodes and then run through them in the macro and display them differently based on type. which I think is the correct way i've done it in the if-statements?

    I just need a way to get the get the Panel Boxes, so i can traverse them ;/

    Thanks again!

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 14, 2013 @ 16:57
    Fuji Kusaka
    0

    I guess each of the content listed here are using different DocTyperight ?

    Try this

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using System.Xml.XPath;
    @using System.Xml;



    @{
             
        List<DynamicNode> panelBoxes = @Model.AncestorOrSelf(1).Descendants("PanelTextBox").Items;
       
       
        foreach(var pages in panelBoxes.Where(x=>x.GetProperty("umbracoNaviHide").Value !="1")){
            if(pages.GetChildrenAsList.Count() > 0 ){
                foreach(var subPages in pages.GetChildrenAsList){
                  
                       if(subPages.NodeTypeAlias =="PanelTextBox" && subPages.Where("Visible")){
                            <a href="@subPages.Url">@subPages.Name</a>
                       }
                      
                       if(subPages.NodeTypeAlias == "PanelRssBox" && subPages.Where("Visible")){
                           XmlTextReader rssReader = new XmlTextReader(subPages.GetProperty("panelRssBoxUrl").Value);
                           XmlDocument doc = new XmlDocument();
                           //
                       }
                      
                       if(subPages.NodeTypeAlias == "PanelImageBox" && subPages.Where("Visible")){
                        
                           //
                       }
                }
            }

          
        }
        
    }

     

  • David Hammer 41 posts 67 karma points
    Oct 14, 2013 @ 17:12
    David Hammer
    0

    Hey again

    Yes, every content panel box is created based on different panel box document types.

    I've tried the answer above, but what my problem is with that solution, is:

    List<DynamicNode>
    

    gives me errors like described 5 posts up, and I can't seem to get it to accept the List collection with DynamicNode as its T argument, do you know why that is?

  • Fuji Kusaka 2203 posts 4220 karma points
    Oct 15, 2013 @ 06:24
    Fuji Kusaka
    0

    What version of umbraco are you using and can you please show the whole code in you razor file ?

  • Funka! 398 posts 661 karma points
    Oct 16, 2013 @ 04:20
    Funka!
    0

    Hi David,

    I wanted to reply to the code sample in your very first post in this thread, to explain your initial error:

    var panelBoxes = Model.DescendantsOrSelf("PanelBoxes").Where("Visible");
    

    In the line above, "panelBoxes" is a list of nodes (DyanmicNodeList) which doesn't really have "children" of its own. (An individual node can have "Children", but a list does not have any "Children", just the contents itself of the list itself.) So instead of getting a list, you need to get a node.

    Probably what you'll find here is if you have only one type of "PanelBoxes" document, then your list here will only have one item in it anyway. (The container node of all your actual panels.) So you could then say you want the "First" result from this list, which is the individual node we need in order to get to the children you want. For example, try changing to this:

    var panelBoxRoot = Model.DescendantsOrSelf("PanelBoxes").First();
    foreach (var panelBox in panelBoxRoot.Children.Where("Visible"))
    {
        ...
    }
    

    Question: is your document type alias for the panel box root actually named "PanelBoxes"?

    Also note, when you "descend" it means you work your way down the tree, so this would still only work if starting at the "Frontpage". The pages such as Articles and Magazines would not find what you're trying to descned into.

    A common solution to this problem is to first "ascend" your tree up to the homepage, then descend to find what you're looking for.

    var panelBoxRoot = Model.AncestorOrSelf(1).DescendantsOrSelf("PanelBoxes").First();
    

    Final notes: Your line of code here has an error:

    if (page.NodeTypeAlias = "PanelTextBox")
    

    Note that this would try to assign a NodeTypeAlias, not compare it. In C#, we need to use double-equal signs when comparing, e.g., if (x == y) and not if (x = y).

    And OK one last note, you do not need the "Any()" method inside your "foreach" loop. The "Any()" method returns a true/false boolean, and you can't loop through a boolean result.

    It's late and I hope some of this helps, sorry if it causes more confusion! Best of luck to you!

  • David Hammer 41 posts 67 karma points
    Oct 18, 2013 @ 15:02
    David Hammer
    0

    I want to thank you both for your time. I needed this short but crusial journey. I now feel like I understand umbraco a lot more. And thank you Funka, the examples given, made me reach the solution.

    Thank you thank you thank you.

Please Sign in or register to post replies

Write your reply to:

Draft