Copied to clipboard

Flag this post as spam?

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


  • mmmoustache 50 posts 79 karma points
    Dec 07, 2011 @ 14:48
    mmmoustache
    0

    Multi-Node Tree Picker: help with example code (in Razor)?

    Hi all,

    Complete beginner to Umbraco & Razor here. I'm using the Multi Node Tree Picker data type to grab some content from selected pages, using Razor. The problem is I can't seem to grab anything from the nodes/pages I've selected. I've used this example for getting values using the data type and nothing is shown:

    <ul>
    @foreach (var x in Model.refKursus) {
      <li>@x.InnerText</li>
    }
    </ul>

    Ideally, I'm trying to get a snippet of body text (via rich text editor), the page url and page title from each of the selected pages, which I can then display on another page.

    So far I've got the datatype installed, and the datatype settings seem to work (from other recommendations). But with the methods I've already tried, nothing is rendering. Obviously as I'm brand new to this, there's a lot of inconsistency in what I've tried so far, but I would really appreciate any suggestions to get this working!

  • Kenny Burns 173 posts 305 karma points
    Dec 07, 2011 @ 15:23
    Kenny Burns
    0

    Hi MM,

    I am a bit of a newbie to this too but have been looking into this a bit as well today and may have found a solution.

     

    Try:

    @{
      var theNode = "";
      foreach (var x in Model.refKursus) {  
      theNode = @x.InnerText;
        var yourItem = @Model.NodeById((@theNode));
        @yourItem.Url
        }
    }

    This (as far as i can gather) is setting the node from the list as the current node from which you can then access your properties?? Does that make sense?

    If anyone else can comment or shed light please do -

    Cheers,

    Kenny

  • Sean Dooley 289 posts 528 karma points
    Dec 09, 2011 @ 13:58
    Sean Dooley
    1

    Hi mmmoustache

    Kenny Burns code looks right. You should be able to grab the node from a single line

    @foreach(var item in Model.refKursus) {
    var node = Library.NodeById(item.InnerText);
    @node.Name @node.Url

     

     

  • Drew 165 posts 340 karma points
    Jan 04, 2012 @ 20:52
    Drew
    0

    Seans example is spot on, here's some example code to list the contents of a "footer menu" property - which itself is a multi-node-picker for content nodes.

     

    @inherits umbraco.MacroEngines.DynamicNodeContext                                                     
                                                         
    @{
      <nav>
        @if(Model.HasValue("footerMenu")){
           <ul>
             @foreach(var item in Model.footerMenu){
                 var node Library.NodeById(item.InnerText);
                 <li>
                  <href="@node.Url" title="@node.Name">@node.Name</a>
                 </li>                                           
             }
           </ul>                           
        }
      </nav>
    }

  • Greg McMullen 49 posts 195 karma points
    Nov 06, 2012 @ 21:18
    Greg McMullen
    0

    This code was fantastic in helping me figure out how to create the multi-node-picker. The only problem I'm having right now is it's not showing up on child pages. The home page is fine, it works great, but as soon as I go to a different page the navigation dissappears.

    I'm using nearly identical code as above (Drew).

    Any advice?

  • Greg McMullen 49 posts 195 karma points
    Nov 06, 2012 @ 22:08
    Greg McMullen
    0

    Got if figured out. Since I'm planning on using this for a main navigational element it was critical to find an answer.

    Here's my code.

    @inherits umbraco.MacroEngines.DynamicNodeContext                                                     
                                                         
    @{
        if(Model.HasValue("topLevelNavigation",true)){
    <nav>
           <ul class="menu">
             @foreach(var item in Model._topLevelNavigation){
                 var node = Model.NodeById(item.InnerText);
                 <li>
                  <a href="@node.Url" title="@node.Name">@node.Name</a>
                 </li>                                           
             }
           </ul>                           
    </nav>
        }
    }
  • Pavel Gurecki 55 posts 158 karma points
    Apr 04, 2013 @ 16:53
    Pavel Gurecki
    0

    Trying to get list of nodes selected by multi-node picker in partial view (umbraco 6 mvc).

    My solution:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{   
    var rootNode = Model.Content.AncestorOrSelf();
    var featuredPostsVal = (Umbraco.Core.Dynamics.DynamicXml)rootNode.GetPropertyValue("featuredPosts"); //this is my MNP field
    //the ugly part
    String[] featuredIds = new String[featuredPostsVal.Count()]; int i = 0; foreach (var featuredPostId in featuredPostsVal) { featuredIds[i] = featuredPostId.InnerText; i++; } var featuredPosts = Umbraco.TypedContent(featuredIds);

    Any ideas how to make it more simple/convenient? 

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 05, 2013 @ 06:01
    Tom Fulton
    0

    Hi,

    You could extract that into a helper method to mask the ugliness :)

    But you should check out PropertyEditorConverters.  Jeavon made a package that has support for a lot of datatypes:  http://our.umbraco.org/projects/developer-tools/umbraco-core-property-editor-converters

    This would let you do the following (taken from docs):

    IEnumerable<IPublishedContent> typedMultiNodeTreePicker = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("mntpFeaturePickerXML");
    foreach (IPublishedContent item in typedMultiNodeTreePicker)
    {      
      <p>@item.Name</p>          
    }

    Hope this helps,
    Tom 

  • Pavel Gurecki 55 posts 158 karma points
    Apr 05, 2013 @ 09:13
    Pavel Gurecki
    0

    Hi Tom,

    PropertyEditorConverters is exactly what I needed.

    Replaced all ugliness with one line :)

    IEnumerable<IPublishedContent> featuredPosts = rootNode.GetPropertyValue<IEnumerable<IPublishedContent>>("featuredPosts"); 

    Thanks!

  • Michael 63 posts 211 karma points
    Mar 19, 2014 @ 11:19
    Michael
    0

    Hi guys

    I am working on a uComponents Multinode tree picker razor. I can't get the razor to fetch the url or the name, but it gets the 3 pages as it should.

     

    UPDATE: Got it working now, it was a wrong setting on my datatype :)

     

    /Michael

     

  • Steve 472 posts 1216 karma points
    May 28, 2014 @ 16:09
    Steve
    0

    Michael,

    What was the solution? I am trying to output pageTitles from nodes that are selected via the uComponents multinode tree picker on a seperate node.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    May 31, 2014 @ 20:19
    Tom Fulton
    0

    Hey Steve,

    I still think the best way to do this is to use the PropertyValueConverters project. Not sure what version you're on, but there's a version specifically for v7 if you're using that. Then you can use the code snippet Pavel posted above.

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Aug 28, 2014 @ 12:24
    Steve Morgan
    1

    Sorry to dredge up an old thread but for folks using Umbraco v7.x I found the following snippet to work in a Partial Macro View

    I have a "promoPanelPicker" which picks content. "Adapted" from http://our.umbraco.org/documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors-v7/Multinode-Treepicker  Hope this helps someone (at least probably me when I Google it again in about two weeks). 

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
    
        if (CurrentPage.HasValue("promoPanelPicker"))
        {
            var promoListValue = CurrentPage.GetPropertyValue<string>("promoPanelPicker");
            var promoList = promoListValue.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    
            @:<h2>@promoListValue</h2>
            @:<h2>Promo panels go here</h2>
            foreach (var item in promoList)
            {
                var promoItem = Umbraco.TypedContent(item);
                <p>@promoItem.Name</p>
            }
    
        }
    }
  • Carlos Mosqueda 244 posts 435 karma points
    Nov 17, 2014 @ 21:31
    Carlos Mosqueda
    0

    @Steve Morgan,

    This was actually perfect for my needs.  Thanks for dredging this up. It helped me out.

Please Sign in or register to post replies

Write your reply to:

Draft