Copied to clipboard

Flag this post as spam?

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


  • BJ Patel 84 posts 210 karma points
    Nov 06, 2015 @ 22:22
    BJ Patel
    0

    Not able to get Node by ID in foreach

    Not able to get value of NodeByID in loop or after passing to @RenderPage("",NodeID);

    As Following is my Scripting File Code

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
       var MenuNode = Library.NodeById(2001);
    
    
        @foreach (var SubItem in MenuNode.Children)
        {
    
            var SubNode Library.NodeById(SubItem.SelectedNode)  // <<   Not able to fetch node 
    
            var SubNode Library.NodeById(3031)     // <<   I have also tried with static Node Id , But was not able to get node value
    
            <h2> @SubNode.Name </h2>  
    
           }
    }
    

    I am not able to fetch the value of node in site foreach.

    As I have try with

    var SubNode Library.NodeById(3031)

    var SubNode @Model.NodeById(3031)

    dynamic SubNode = new umbraco.MacroEngines.DynamicNode(3130);

    etc.

    but I am not able to get the value of Node. by ID in foreach loop.

    so how can I access it ?

    Thanks , BJ

  • Martin 114 posts 313 karma points
    Nov 07, 2015 @ 11:22
  • BJ Patel 84 posts 210 karma points
    Nov 09, 2015 @ 08:29
    BJ Patel
    0

    Hi Martin,

    Thanks for your reply,

    I have tried with UmbracoHelper , it's working when I give static value .. :)

     @foreach (var SubItem in MenuNode.Children)
            {        
                 var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current)
    
                 var nodeContent = umbracoHelper.Content(2001);    < Passing static value it is working.
    
                <h2> @SubNode.Name </h2>          
              }
    

    But when I pass content picker value (SubItem.SelectedNode) , do not work :(

    @foreach (var SubItem in MenuNode.Children)
            {        
                 var umbracoHelper = new Umbraco.Web.UmbracoHelper(Umbraco.Web.UmbracoContext.Current)
    
                 var nodeContent = umbracoHelper.Content(SubItem.SelectedNode);    < have also try with converting to int.
    
                <h2> @SubNode.Name </h2>          
              }
    
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 09, 2015 @ 09:15
    Dennis Aaen
    0

    Hi BJ

    You should be able to do something like this to get it it to work.

    @foreach (var SubItem in MenuNode.Children){       
    
        var node = Umbraco.Content(SubItem.SelectedNode);
    
                <h2> @SubItem.Name </h2>          
    }
    

    You can find all the built in property documentation here. https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/ including the content picker

    Hope this helps,

    /Dennis

  • BJ Patel 84 posts 210 karma points
    Nov 09, 2015 @ 09:41
    BJ Patel
    0

    Thanks Dennins,

    But When I inject following code

    var node = Umbraco.Content(SubItem.SelectedNode);
    

    I am getting following error. Msg.

    "The type or namespace name 'Content' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)"

    FYI : I am using umbraco 6

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 09, 2015 @ 09:54
    Dennis Aaen
    0

    Hi BJ,

    What is the first line of your Razor file, something like this.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    

    When we know this it´s much easier to help you in the right directions.

    /Dennis

  • BJ Patel 84 posts 210 karma points
    Nov 09, 2015 @ 10:04
    BJ Patel
    0

    Hi Dennis,

    it's

    @inherits umbraco.MacroEngines.DynamicNodeContext

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 09, 2015 @ 10:20
    Dennis Aaen
    0

    Hi BJ

    Then you should be able to do something like this,

    <ul>
        @foreach(var subItem in MenuNode.Children){
    
            @*For each link, get the node, and display its name and url*@
            var node = Library.NodeById(subItem.selectedNode);
    
            <li><a href="@node.Url">@node.Name</a></li> 
        }
    </ul>
    

    Make sure that the alias of your content picker is correct, if you don´t change it then it will take the name of your property, starting with lowercase.

    Hope this helps you in the right direction,

    /Dennis

  • BJ Patel 84 posts 210 karma points
    Nov 09, 2015 @ 10:38
    BJ Patel
    0

    Hi Dennis,

    No it's not working for me :(

    As per my first post I am able to get node using

    Library.NodeById(2001);
    

    But outside the scope of foreach...!

  • Eran Schoellhorn 3 posts 74 karma points
    Nov 12, 2015 @ 16:27
    Eran Schoellhorn
    0

    Hey BJ!

    I just stumbled onto this thread while trying to solve a similar (exact) problem. I came across a few things:

    1. Library.NodeById is deprecated. Reference.
    2. In lieu of the above, use Umbraco.Content() to retrieve the node in question.
    3. The node ID must be cast as int in order for Umbraco.Content to function correctly.

    Here's an abbreviated application of this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @foreach(var item in tiles)
    {
        var node = Umbraco.Content( (int)item.link );
        @node.Url
    }
    

    Hopefully this helps you as it has me. I was stumped on this issue for a bit.

    Good luck!

    UPDATE:

    This works a little unusual as this cast actually causes the partial to fail when rendering if it's applied outside of the foreach loop.

Please Sign in or register to post replies

Write your reply to:

Draft