Copied to clipboard

Flag this post as spam?

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


  • Biagio Paruolo 1593 posts 1824 karma points c-trib
    Jan 19, 2018 @ 12:34
    Biagio Paruolo
    0

    Solved - How to get the page id of the current nested content?

    How to get the page id of the current nested content?

  • Kevin Jump 2310 posts 14695 karma points MVP 7x c-trib
    Jan 19, 2018 @ 13:17
    Kevin Jump
    2

    Hi

    Nested Content Items are 'detached' content items which means they have no relationship back to their parent or other bits of content. So the answer is you can't the ID of the page the nested content is on directly from the nested Content Item : (

    you can get the current page from the UmbracoContext.

    @UmbracoContext.Current.PageId
    

    but it is probably best practice to pass the page id (or the things you need) to any partial that handles the nested content

     @Html.Partial("nestedPartial", nestedItem,  new ViewDataDictionary {
                                                { "CurrentPageId", Model.Id }
                                            })
    

    you can then get this in the parial . with ViewData["CurrentPageId"]

    if you need to access the current page model - then it's probibly better to actually pass that to the partial and get the NestedContent Items within the partial ? - or at least pass the model as well as the nested content - that way you won't have to do re-lookup the page model in your code.

  • Biagio Paruolo 1593 posts 1824 karma points c-trib
    Jan 19, 2018 @ 14:43
    Biagio Paruolo
    100

    I solved the problem with this workaroud. I create in app_code a my class:

    u

    sing  System;
    using  System.Collections.Generic;
    using  System.Linq.Expressions;
    using  System.Web;
    using  Umbraco.Core.Models;
    using  Umbraco.Core.Models.PublishedContent;
    using  Umbraco.Web;
    using  Umbraco.ModelsBuilder;
    using  Umbraco.ModelsBuilder.Umbraco;
    
    namespace RedConsulting
    {
         public class RedPoint
         {
            public IPublishedContent point{get; set;}
            public int nodeID {get; set;}                           
    
         }
    }
    

    Then into the page:

    List<RedPoint> elenco = new List<RedPoint>();
    
    var selectionPercorsi = Model.Content.Site().FirstChild("products").Children("product")
                            .Where(x => x.IsVisible());
    
    
        foreach(var item in selectionPercorsi){
    
           var points = item.GetPropertyValue<IEnumerable<IPublishedContent>>("points");
           int nodeid = item.Id;
    
           foreach(IPublishedContent itemp in points)
              {
                 RedPoint rp = new RedPoint();
    
                 rp.point = itemp;
                 rp.nodeID= nodeid;
    
                 elenco.Add(rp);
    
              }
        }
    

    The rp.nodeID contain the ID of "parent" and then you can access data with var nodeParentContent = Umbraco.Content(itemrp.nodeID);

Please Sign in or register to post replies

Write your reply to:

Draft