Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    Feb 06, 2013 @ 16:41
    Steve
    0

    Navigation Using a Consistent Value for The Top Level Node

    I am trying to use the same navigation on all children of a node within a site using razor. The problem is that I don't know how to write the code so that even the children will have all the same navigation that the top most node would have. @Model (current node) changes acording to which page you are on and then the children no longer have the same top level node descendants, so no navigation is displayed. I hope that is understandable. Here is my code:

    @{

    var listings = Model.DescendantsOrSelf();
    var listGroup = listings.Count()/3;

    <div class="header-nav">
                   @foreach (var group in listings.InGroupsOf(@listGroup)) {
          <ul class="column">
                   @foreach (var item in group) {
            <li><a href="@if ( item.url != "") { @item.url; } else { @item.Url; }">
                   @if (item.HasValue("shortPageTitle") ) {
                   @item.shortPageTitle;
                   } else if (item.HasValue("pageTitle") ) {
                   @item.pageTitle;
                   } else {
                   @item.Name;
               }
              </a></li>
           }    
         </ul>
        }
    </div>

    }

     

  • Steve 472 posts 1216 karma points
    Feb 06, 2013 @ 17:55
    Steve
    0

    Why doesn't this work to start my navigation at the node "CourseCatalogHome"?

    var listings = Model.Descendants().HasProperty("CourseCatalogHome");

  • gary 385 posts 916 karma points
    Feb 06, 2013 @ 19:39
    gary
    0

    Hi Steve

    Without knowing which version you are using, the caode maybe different.

    This will work with Mvc as opposed to webforms, but the theory is the same.

    To get to root node;

    var root= CurrentPage.AncestorOrSelf() Will climb to to top node in the tree, thus making navigation equal on each page.

    then foreach ( var item in root.Children("Add your child doctype alias"))

    or you can put it all together as var root = CurrentPage.AncestorOrSelf().Children("Add your child doctype");

     foreach (var item in root)

    As I said it maybe slightly different in webforms, but if you follow the theory you should get there.

    Hope it helps

    Best regards

    Gary

     

     

  • Steve 472 posts 1216 karma points
    Feb 06, 2013 @ 19:56
    Steve
    0

    Gary,

    I have tried what you suggested with no results. What am I missing? I've been on this for hours.

     

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    
    var listings = Model.AncestorsOrSelf().Children("CourseCatalogInternal");
    var listGroup = listings.Count()/3;
    
        <div class="header-nav">
            @foreach (var group in listings.InGroupsOf(@listGroup)) {
        <ul class="column">
            @foreach (var item in group) {
                <li><a href="@if ( item.url != "") { @item.url; } else { @item.Url; }">
                    @if (item.HasValue("shortPageTitle") ) {
                    @item.shortPageTitle;
                } else if (item.HasValue("pageTitle") ) {
                    @item.pageTitle;
                } else {
                    @item.Name;
                }
             </a></li>
            }                      
        </ul>
            }
        </div>
  • gary 385 posts 916 karma points
    Feb 06, 2013 @ 20:05
    gary
    0

    Steve

    Will try to help all I can, but sorry don't use webforms so can only advise, but believe me I have been where you are many times.
    My first thought is to simplify, strip out the if statements, just get the call right, from memory, you may need descendants rather than children and without seeing your tree its a bit of guesswork. Could also try AncestorOrSelf (ie leave out the "s")
    Just thought that I have a live site in 4.7 which is in webforms although I just direct straight out to a cshtml file, but I will have a look in there to see what I can find.
    Will have a look now and see if I can find something to help - been working with InDesign all day, will be a relief.
    Regards
  • gary 385 posts 916 karma points
    Feb 06, 2013 @ 20:19
    gary
    0

    HI

    Just checked, both used @Model.AncestorOrSelf().Children() so it is the singular Ancestor and Children. 

    One other thought is that if your parent node is not the top level, try AncestorOrSelf(1).Children().

    Check your docType name if camel case, ie needs to be courseCatalog . . . . the first letter will not be a capital, this has caught me in the past.

    Sorry I can't be more specific, but hope something will help.

    Gary

     

  • Steve 472 posts 1216 karma points
    Feb 06, 2013 @ 21:12
    Steve
    0

    Got it. Thanks for your help! I had to seperate root from the Descendants to get it to work. Don't know why.

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    
    var root = Model.AncestorOrSelf();
    var listings = root.Descendants("CourseCatalogInternal");
    var listGroup = listings.Count()/3;
    
        <div class="header-nav">
            @foreach (var group in listings.InGroupsOf(@listGroup)) {
        <ul class="column">
            @foreach (var item in group) {
                <li><a href="@if ( item.url != "") { @item.url; } else { @item.Url; }">
                    @if (item.HasValue("shortPageTitle") ) {
                    @item.shortPageTitle;
                } else if (item.HasValue("pageTitle") ) {
                    @item.pageTitle;
                } else {
                    @item.Name;
                }
             </a></li>
            }                      
        </ul>
            }
        </div>
    }
  • gary 385 posts 916 karma points
    Feb 06, 2013 @ 21:22
    gary
    0

    Cool!

    Now you say it, yes, my understanding is that Children is only on static, but you need descendants for dynamic (or something similar), and yes, have had to split them in the past.
    A lot of the "razor walkthrough" series is still valid, as is the razor node cheat sheet (as far as I am aware), usually refer to these if seeking a bit of inspiration.
    Glad you got it sorted. On to the next!
    Regards G
  • Roey 5 posts 25 karma points
    Feb 20, 2013 @ 16:42
    Roey
    0

    Hi gary/steve,

    can you experts please help with a very small thing,

     

    I'm new to this and just wanted to know "the number of levels of navigation" (from root level) by using some method or function

    I tried "Model.Descendants(1).Last()" but that resulted in getting the last node of my site, but I want the final level (deepest level)

    please visit this link to understand my query clearly

    http://our.umbraco.org/forum/developers/razor/38566-how-to-get-the-deepest-level-child-of-Home-page

     

    thanks

    Roey

Please Sign in or register to post replies

Write your reply to:

Draft