Copied to clipboard

Flag this post as spam?

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


  • lucuma 261 posts 563 karma points
    Feb 28, 2011 @ 03:16
    lucuma
    1

    Razor Snippet Top Navigation is Incorrect

    I tried to fix the wiki page but wasn't able to save it for whatever reason.  The snippet Top Navigation (using DynamicNode) is incorrect.  I kept running my site and wondering why my IIS installation was crashing until I looked at what I pasted from that snippit.  currentPage should be set to its parent and not Model.Parent otherwise if you go two levels deep it will crash your site.  Can someone make that change to help out others.

    @{
      var currentPage = Model;
      while (currentPage.Level>2) currentPage=currentPage.Parent; // ** changed
      <ul class="topnavigation">
      @foreach (var c in currentPage.Children)
      {
       <li><a href="@c.Url">@c.Name</a></li>
       }
      </ul>
     
      }

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 28, 2011 @ 11:35
    Sebastiaan Janssen
    1

    Yeah, I've tried to correct that in the wiki as well, the umbraco team is working on fixing the wiki save problem! Don't forget the curlies on that same line by the way, they're required as of 4.7+

    @{
      var currentPage = Model;
      while (currentPage.Level>2) { currentPage=currentPage.Parent; } // ** changed 
      <ul class="topnavigation">
      @foreach (var c in currentPage.Children)
      {
       <li><a href="@c.Url">@c.Name</a></li>
       }
      </ul>  
    }
  • Allan Michaelsen 14 posts 33 karma points
    Mar 01, 2011 @ 09:30
    Allan Michaelsen
    0

    I have two questions for this one:

    1. If i want to take into account the umbracoNaviHide property it would make sense to use .Where("umbracoNaviHide"), but since i need it to be false, how do i then negate that one? Tried various ways, but the result is alwys "can't cast bool to..."
    2. In my menu (the new theme for 4.7) the current menu gets highlighted. This works like a treat with the code i have, but only on the first same level. If i move into an image album, the highlight disappears as the current page id isn't the same as the id's i run through in my foreach. I tried using the.Ancestors to travers to level 2 (my menu starting point), but to no avail.
    <ul id="topNavigation">
    @{
      var level = 2;
      var currentPage = Model;
      while (@currentPage.Level > @level-1){ currentPage = @currentPage.Parent; }

      foreach(var page in @currentPage.Children.Where("umbracoNaviHide"))
      {
        if(@page.Level == @level)
        {
          if(@page.Id == @currentPage.Id)
          {
            <li class="current">
              <a class="navigation" href="@page.Url">@page.Name</a>
            </li>
          }
          else
          {
            <li>
              <a class="navigation" href="@page.Url">@page.Name</a>
            </li>
          }
        }
      }
    }
    </ul>

    Hope you can point me in the right direction.

    Regards

    Allan

  • lucuma 261 posts 563 karma points
    Mar 01, 2011 @ 14:55
    lucuma
    0

    You could try something like this (not tested in any way and not very elegant either):

    bool hide = false;
    
    foreach(var page in @currentPage.Children) {
        hide = false;
        try {hide=page.umbracoNaviHide } catch { }
        if (! hide) {
         // do whatever
        } else { }
    
    }
  • Damiaan 442 posts 1302 karma points MVP 6x c-trib
    Mar 01, 2011 @ 17:24
    Damiaan
    0

    I am using this for the umbracoNaviHide

      @foreach (var c in currentPage.Children)
      {
        if(c.umbracoNaviHide.ToString() == "1") {continue;}
       <li><a href="@c.Url">@c.Name</a></li>
      }

  • Yannick Smits 321 posts 718 karma points
    Mar 01, 2011 @ 20:05
    Yannick Smits
    0

    according to http://umbraco.com/follow-us/blog-archive/2011/3/1/umbraco-razor-feature-walkthrough-%E2%80%93-part-4 you can just do .Where("umbracoNaviHide != true") as per 4.7

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 02, 2011 @ 00:51
    Jonas Eriksson
    0

    Ah, finally the wiki is writeable and I could correct my mistake, plus make it look better. Missing properties are handled gracefully now, so this will not throw an error if one of the children is of a doctype without the umbracoNaviHide property.

    @{
      <ul class="topnavigation">
      @foreach (var c in Model.AncestorOrSelf(1).Children.Where("umbracoNaviHide!=true")
      {
       <li><a href="@c.Url">@c.Name</a></li>
       }
      </ul>
      }


    Would be nice to add an HasAccess() also in the where - but that was not possible atm, could be added within the loop tho if (c.HasAccess())...

  • Allan Michaelsen 14 posts 33 karma points
    Mar 02, 2011 @ 12:34
    Allan Michaelsen
    0

    I just upgraded to RC and it works fine. The solution i ended up with is this:

    @* --- Module --- *@
    <ul id="topNavigation">
    @{
      var level = 2;
      var currentMenu = @Model.AncestorOrSelf(@level);

      foreach(var page in @Model.AncestorOrSelf(@level-1).Children.Where("umbracoNaviHide==false"))
      {
        if(@page.Id == @currentMenu.Id)
        {
          <li class="current">
            <a class="navigation" href="@page.Url">@page.Name</a>
          </li>
        }
        else
        {
          <li>
            <a class="navigation" href="@page.Url">@page.Name</a>
          </li>
        }
      }
    }
    </ul>

    I think it's pretty good, but if anyone has a better way let me know.

  • Jonas Eriksson 930 posts 1825 karma points
    Mar 02, 2011 @ 13:05
    Jonas Eriksson
    0

    Very good! You could add this expression to shorten it:

    <li @(page.Id == currentMenu.Id?"class=current":"")> 
     
    <a class="navigation" href="@page.Url">@page.Name</a>
    </li>

     

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 23, 2011 @ 10:39
    Biagio Paruolo
    0

    I've runtime NULL error on this:

    <ul id="topNavigation">
    @{


      if (@Model.Level>=2) {
                          
          var level = 3;
      var currentMenu = @Model.AncestorOrSelf(@level);                  
      foreach(var page in @Model.AncestorOrSelf(@level-1).Children.Where("umbracoNaviHide==false"))
      {
        if(@page.Id == @currentMenu.Id)
        {
          <li class="current">
            <a class="navigation" href="@page.Url">@page.Name</a>
          </li>
        }
        else
        {
          <li>
            <a class="navigation" href="@page.Url">@page.Name</a>
          </li>
        }
      }
      }
    }
    </ul>

    I wish to list first children of current node.

    Thanks

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 23, 2011 @ 11:22
    Sebastiaan Janssen
    0

    You have too many @ signs, if you're in a code block (@{ .. }) you don't need to use @ unless you want to write something to the page:

    <ul id="topNavigation">
    @{
      if (Model.Level>=2) {
    
          var level = 3;
      var currentMenu = Model.AncestorOrSelf(level);                  
      foreach(var page in Model.AncestorOrSelf(level-1).Children.Where("umbracoNaviHide==false"))
      {
        if(page.Id == currentMenu.Id)
        {
          <li class="current">
            <a class="navigation" href="@page.Url">@page.Name</a>
          </li>
        }
        else
        {
          <li>
            <a class="navigation" href="@page.Url">@page.Name</a>
          </li>
        }
      }
      }
    }
    </ul>
  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 23, 2011 @ 12:15
    Biagio Paruolo
    0

    same error...

    My SiteMap is:

    Umbraco Node Content ( level 0 )

         IT - level 1

               Node A ( level 2 )

                   Node A1 ( level 3 )

              Node B ( level 2 )

    I've a fix menu with link of level 2 node. Then when I click on one of them, must be appear the list of links to child of level 3 for that node of level 2

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 23, 2011 @ 12:29
    Sebastiaan Janssen
    0

    What is the exact error message that you're getting? Have you copied and pasted the code I provided exactly? It works perfectly for me.

    Try and remove the .Where("umbracoNaviHide==false") and see if it works then.

    If that does not work you're going to have to find out what exactly is null, so experiment with leaving bits of code out and see if the error still occurs.

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 23, 2011 @ 14:37
    Biagio Paruolo
    0

    Yes. I paste it as you wrote. I delete Where, but I've the same error Impossibile eseguire un'associazione di runtime su un riferimento Null


  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 23, 2011 @ 14:44
    Sebastiaan Janssen
    0

    So, what happens when you start commenting out code, at which line does it break? You could also set a breakpoint at the beginning and step through the code to see when it breaks.

  • Biagio Paruolo 1618 posts 1910 karma points c-trib
    Mar 23, 2011 @ 15:05
    Biagio Paruolo
    0

    ??? But I don't use Visual Studio, now. If I wish to use, how connect for debugging ?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 23, 2011 @ 15:07
    Sebastiaan Janssen
    0

    Have a look at this blog post: http://blog.percipientstudios.com/2011/3/3/how-to-umbraco-razor-intellisense-and-debugging.aspx

    You could also just start commenting out code until the error disappears.

  • Tom 713 posts 954 karma points
    Apr 10, 2012 @ 10:29
    Tom
    0

     

        var rootNodes = currentPage.AncestorsOrSelf.Last().Children;

            

        <nav role="navigation" class="nav-collapse">

            <ul class="nav">

                @foreach(var rootNode in @rootNodes) {

                    bool hasChildren = rootNode.Children.Where("umbracoNaviHide != @0", "True").Any();

                    @Html.Raw(rootNode.Children.Where("umbracoNaviHide != @0", "true").Count().ToString())

                <[email protected](hasChildren ? "class=\"dropdown\"" : "")>

                    @* Add CSS class selected if rootIsSelected *@

                    <a href="@rootNode.Url" class="@(currentPage.Id == rootNode.Id ? "selected" : "")">@rootNode.Name</a>

     

                    @* Display children on  rootNode *@

                    @childPages(rootNode.Children, maxLevel)

                </li>

                }

            </ul>

        </nav>

     

     

    @Html.Raw(rootNode.Children.Where("umbracoNaviHide != @0", "true").Count().ToString())

     

    I have the code above.. why does the count always return 0... it doesn't seem like the where clause with umbracoNaviHide works at all...

Please Sign in or register to post replies

Write your reply to:

Draft