Copied to clipboard

Flag this post as spam?

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


  • Erma Isabel 20 posts 91 karma points
    Nov 06, 2013 @ 11:33
    Erma Isabel
    1

    Get node by id and chidren

    Hi all,

    I am trying to get node by hard coded id and then its children.

    I tried the following code,

        dynamic node = new DynamicNode(1052);
        <p>@node.Id</p> // output is 1052
        <p>@node</p>
    
        foreach(var item in @node.Children().Where("Visible"))
        {
            <p>@item.movingClean</p> // output of this line is "System.Collections.Generic.List`1[System.String[]]"
       //expected output is the values from each children corresponding to the alias "movingClean" 
        //"movingclean" is alias of textstring array
        }
    

    Working code without using dynamic node is as follows:

        foreach (var item in CurrentPage.Children().Where("Visible"))
        {
            var count = 0;
            foreach (var counter in item.movingclean)
            {
             count = count + 1;
            }
            for (int i = 0; i < count; i++){
                foreach (var qLoop in item.movingclean[i]){
                    <p>@qLoop.InnerText</p> 
                }
            }
        }
    

    umbraco 6.1.6

    How can i get the items in children?

    Please help,

    Thanks

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 06, 2013 @ 11:40
    Dennis Aaen
    0

    Hi Erma,

    Try this one instead:

    @foreach(var item in Model.NodeById(1052).Children().Where("Visible")){
     
    <p>@item.movingClean</p>
    }

    Hope this helps

    /Dennis

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 06, 2013 @ 11:40
    Jeavon Leopold
    1

    Hi Erma,

    Try this:

    @{
        var node = Library.NodeById(1052);    
        <p>@node.Id</p> // output is 1052
        <p>@node</p>
    
        foreach (var item in node.Children.Where("Visible"))
        {
            <p>@item.movingClean</p> // output of this line is "System.Collections.Generic.List`1[System.String[]]"
            //expected output is the values from each children corresponding to the alias "movingClean" 
        }
    }
    

    Jeavon

  • Fuji Kusaka 2203 posts 4220 karma points
    Nov 06, 2013 @ 11:48
    Fuji Kusaka
    0

    Try something like

    foreach(var item in node.ChildrenAsList.Where(x => x.GetProperty("umbracoNaviHide").Value != "1"));
        {
    var file = itemm.getProperty("movingClean").Value;
    if(file !=""){
    <p>@file</p>
    }       

    }
  • Erma Isabel 20 posts 91 karma points
    Nov 06, 2013 @ 12:44
    Erma Isabel
    1

    @Jeavon

    Do i need to add any assembly reference for Library?

    @Dennis

    Following error occurred when i tried the code,

    CS1061: 'Umbraco.Web.Models.RenderModel' does not contain a definition for 'NodeById' and no extension method 'NodeById' accepting a first argument of type 'Umbraco.Web.Models.RenderModel' could be found (are you missing a using directive or an assembly reference?)
    

    @fiji

    Error occurs :-(

    CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 06, 2013 @ 12:51
    Jeavon Leopold
    105

    Hi Erma,

    Actually your error message indicates you are using Mvc, however your code samples are for non Mvc Razor Macros, try this instead:

    @{
        var node = Umbraco.Content(1052);    
        <p>@node.Id</p> // output is 1052   
        foreach (var item in node.Children.Where("Visible"))
        {
            <p>@item.movingClean</p> // output of this line is "System.Collections.Generic.List`1[System.String[]]"
            //expected output is the values from each children corresponding to the alias "movingClean" 
        }
    }
    

    Jeavon

  • Erma Isabel 20 posts 91 karma points
    Nov 06, 2013 @ 12:54
    Erma Isabel
    0

    @Jeavon

    Thank you thank you thank you

    It worked :-)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 06, 2013 @ 12:59
    Jeavon Leopold
    0

    You're welcome! It's a bit confusing with MVC & non MVC Razor support in Umbraco, when posting a topic here it's worth saying you are using MVC (or not) as it will help get you to the answer :-)

  • Erma Isabel 20 posts 91 karma points
    Nov 07, 2013 @ 05:10
    Erma Isabel
    0

    @Jeavon

    One more doubt.

    What need to be changed to make the above code work in a partial view?

    Let me clarify my question,

    My partial contains following model and assemblies,

    @model MvcApplication2.Models.ContactUs
    @using umbraco.MacroEngines;
    @using umbraco.NodeFactory;
    

    Since i am using model, i couldn't be able to inherit

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    

    How can i make above code work here?

    Please help,

  • Funka! 398 posts 661 karma points
    Nov 07, 2013 @ 23:40
    Funka!
    1

    If you're using MVC in version 6, I question whether you need the umbraco.MacroEngines and umbraco.NodeFactory namespaces at all?

    But to answer your question, it seems you want to use a custom model in your partial view, but still have access to the umbraco helpers and nodes, etc.

    Try @inherits Umbraco.Web.Mvc.UmbracoViewPage<YourCustomModel> and you should still be able to call upon Umbraco helper. Hopefully I did not misunderstand what you are trying to do... If so could you post how you are trying to include your partial?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Nov 08, 2013 @ 08:08
    Jeavon Leopold
    0

    Yes, the answer is exactly as Funka has suggested and also he is right to say you shouldn't be referencing those namespaces either, they are not needed and would potentially cause you issues.

  • Brian McNally 17 posts 105 karma points
    Jun 04, 2014 @ 18:43
    Brian McNally
    1

    Jeavon...that's twice so far this month your Umbraco mastery has helped me out...THANK YOU

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 05, 2014 @ 15:30
    Jeavon Leopold
    0

    Hi Brian,

    Brilliant, even better when the answer already exists!

    Jeavon

  • Arjun Singh 15 posts 86 karma points
    Mar 08, 2018 @ 09:18
    Arjun Singh
    0

    This is working with umbraco 6 and version 7 + as well , let me know if it works for you.

    var tcnode = new DynamicNode(new umbraco.NodeFactory.Node(nodeId));
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 08, 2018 @ 10:53
    Jeavon Leopold
    0

    Arjun, this is a very old thread you comment on, however you shouldn't ever be using DynamicNode in Umbraco v7

Please Sign in or register to post replies

Write your reply to:

Draft