Copied to clipboard

Flag this post as spam?

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


  • John 115 posts 150 karma points
    Nov 03, 2012 @ 03:13
    John
    0

    Noob cannot get @Model.NodyById() to work

    Hello there, I am a noobie at writing razor scripts and I am struggeling to get the following code to work in Umbraco 4.7.1;

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{ 
      var level = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.Level); 
      var ulClass = String.IsNullOrEmpty(Parameter.UlClass) ? "" : String.Format(" class=\"{0}\"", Parameter.UlClass); 
      
      var rootNode = @Model.NodyById(2278);
      var parent = rootNode.AncestorOrSelf();
         
      if (parent != null) {
        <ul id="secondarynav" >
        @foreach (var item in  parent.Children.Where("!HideFromHorizontalMenu")) {                                                                  
          var isFirst = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
          var isLast = Array.IndexOf(Model.Path.Split(','), item.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
          <li@Html.Raw(isFirst)>
            <a href="@item.Url">@item.Name</a>                  
          </li>
          }
        </ul>                    
      }
    }

    The script is meant to create a navigation menu. It is a modified version of the navigation script that comes with the initial umbraco install. For the life of me, I just cannot seem to get it to start from the particular node I want it to.

    If anyone has any ideas, I would be very appreciative. Thank you for looking.

  • Fuji Kusaka 2203 posts 4220 karma points
    Nov 03, 2012 @ 07:52
    Fuji Kusaka
    0

    Hi John,

    Are you getting any output from the code above? 

    Can you try changing 

    var parent = rootNode.AncestorOrSelf() ; to var parent = rootNode.AncestorOrSelf(1).Descendants("docTypeName");

    And if you dont mind you could show what your structure looks like and what should be the output

    //fuji

  • John 115 posts 150 karma points
    Nov 03, 2012 @ 14:02
    John
    0

    Hi Fuji, thanks so much for spending your time for me.

    I gave that a try and still the same :(. No output at all.

    My content tree looks like follows;

    Also as an addition, note that I have found the following code works in that it will loop through with the foreach, but it always starts from the ancestor above the current page...which is not really what I want :(

    var  parent = @Model.AncestorOrSelf(1);

     

    The output should be the navigation secondary node and children nodes in an unordered list. At least, that is the intention :) Here is some sample code;

     <ul id="secondarynav">
            <li class="first"><a href="#">Project Management</a></li>
            <li><a href="#">Book Consultation</a></li>
            <li><a href="#">Client Login</a></li>
            <li><a href="#">FAQs</a></li>
            <li class="last"><a href="#">Contact Us</a></li>
          </ul>
         

     

    Thanks again so much for having a look. Im new to it, and Im finding it very frustrating that I can't do this! One would think it is simple :)

     

     

     

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Nov 04, 2012 @ 03:10
    Bo Damgaard Mortensen
    0

    Hi John,

    Have you tried Model.NodeById() instead of Model.NodyById() ? :-)

  • Fuji Kusaka 2203 posts 4220 karma points
    Nov 04, 2012 @ 08:58
    Fuji Kusaka
    0

    Hi John,

    Sorry i didnt notice you miss spelll NodeById. Anyways let try doing it this way

    @{
    var parent = Model.NodeByid(2278);
     var items = parent.Children;

    if(parent.Children.Where("Visible").Count() > 0){
    /// here you can check if you have childnodes if they are visible assuming you are using property umbracoNaviHide
    <ul id="secondaryNav">
    @foreach(dynamic nodes in items){
    if(nodes.IsFirst()){
       <liclass="first"><ahref="#">@nodes.Name</a></li>
    }
    else if(nodes.IsLast()){
        <liclass="last"><ahref="#">@nodes.Name</a></li>
    }
    else{
    <li><a href="#">@nodes.Name</a></li>
    }
    }
    </ul>
    }
    }

    Hope this helps

     

    //fuji

  • John 115 posts 150 karma points
    Nov 04, 2012 @ 12:16
    John
    0

    Oh, How embarassing :)  Yes, the problem was NodyById! Bad typing :) 

    I can't believe I spent hours on this one!

    Thank you Bo and Fuji, so much appreciate your help, honestly I was almost ready to pack it in and become a sales guy instead :) 

    Also, I haven't tried it yet Fuju, but your rewritten code is easier to read than mine, I will have a play with it now. I may learn some things.

    I am currently editing in a browser because I've never figured out how to easily deploy stuff from visual studio. Perhaps that would have preventedwhat should have been an obvious mistake.

    Thank you guys.

    John 

Please Sign in or register to post replies

Write your reply to:

Draft