Copied to clipboard

Flag this post as spam?

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


  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 12, 2011 @ 22:53
    Kim Andersen
    0

    Problems with Razor navigation in v5 Beta 1

    Hi there folks

    I've just started learning Razor, as I've been using XSLT to all of my stuff until now :)

    But I have a couple of weird problems trying to modify the navigation.cshtml that comes with v5 Beta 1.

    This is the code that's used to render the navigation in the default site in v5 beta 1:

    @inherits RenderViewPage
    @using Umbraco.Cms.Web;

    @{
    var Homepage = @DynamicModel;
    while (Homepage.ContentType.Alias != "homePage")
    {
    Homepage = Homepage.Parent;
    }
    }
    <ul>
    <li><a href="@Homepage.Url">Home</a></li>
    @foreach (var item in Homepage.Children)
    {
    if (@item.CurrentTemplate != null)
    {
    var childName = item.Name ?? "(No name yet)";
    <li class=""><a href="@item.Url">@childName </a></li>
    }
    }
    </ul>

    I have watched some of the Razor videos on umbraco.tv and wanted to ad a class to the current active <li>, and besides that I only wanted the menu to show nodes where the umbracoNaviHide is not checked. Just like Warren shows in one of the videos. But unforunately it doesn't work :(

    First of, I wanted to only show nodes where umbracoNaviHide os not checked, so I changed the foreach to this:

      @foreach (var item in Homepage.Children.Where("Visible"))

    But this throws a YSOD saying: "'object' does not contain a definition for 'Where'".

    I also tried putting on a class on the active <li> usign this code:

    <li class="@Library.If(Homepage.Id == Model.Id, "selected", "")"><a href="@item.Url">@childName </a></li>

    But this gives me the folling error:

    "Compiler Error Message: CS0103: The name 'Library' does not exist in the current context"

    Any help will be highly appreciated :)

    Thanks in advance folks...!

    /Kim A

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 12, 2011 @ 22:57
    Bo Damgaard Mortensen
    0

    Hi Kim,

    Just a shot in the dark, but could it be a missing reference to: umbraco.MacroEngines.Library ? :-)

  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 12, 2011 @ 23:08
    Kim Andersen
    0

    Hi Bo

    Just tried inserting this:

    @using Umbraco.MacroEngines.Library;

    But then I get this error:

    "Compiler Error Message: CS0234: The type or namespace name 'MacroEngines' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)"

    /Kim A

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 12, 2011 @ 23:25
    Bo Damgaard Mortensen
    0

    Hmm, sorry - just realized that was for 4.7.1 :( Trying to make use of the razor library myself atm with no luck. Will post here if I find a solutions to it (I'm sure you will do the same :))

  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 12, 2011 @ 23:30
    Kim Andersen
    0

    Yes sir, I will for sure :)

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Nov 12, 2011 @ 23:54
    Bo Damgaard Mortensen
    1

    One way of doing it in v5 beta1:

    if(item.Id == Model.Id)
    {
        <li class="selected"><a href="@item.Url">@childName </a></li>
    }
    else
    {
        <li><a href="@item.Url">@childName </a></li>
    } 

    A bit clumsy though, should be a way to write it in-line :) 

  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 13, 2011 @ 00:15
    Kim Andersen
    0

    Ahh, yeah that seems to work. But as you say there should be a smarter and more pretty way of achieving this.

    Small steps towards the final solution is also great though :)

    Thank you so far Bo :)

    /Kim A

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Nov 17, 2011 @ 20:00
    Sebastiaan Janssen
    1

    There is:

    <li class="@(item.Id == Model.Id ? "selected" : "")"><a href="@item.Url">@childName </a></li>

    How about them apples!

    For the record, this is something called a ternary operator.

  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 17, 2011 @ 22:26
    Kim Andersen
    0

    Great Sebastiaan, that was a bit more elegant :) - Thanks!

    You have some inputs on the problem about the .Where("Visible")-part I menitoned above?

    /Kim A

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Nov 18, 2011 @ 10:52
    Sebastiaan Janssen
    0

    I missed that you're using v5. I don't know what the equivalent of "Visible" is but I think it's just not been built in yet. There won't be a 1:1 translation of the v4.7.1 razor to v5 though, so don't rely on the "old" version. If you need to get any razor examples then just google for the mvc way to do things, not the umbraco way to do it (as there is no guidance out there yet). The only way to know how to do it in v5 is by looking at the built in samples.

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Nov 18, 2011 @ 10:57
    Sebastiaan Janssen
    0

    Oh.. maybe you're not using v5, your opening post is confusing dude. 

    If Library is missing then you're probably using 4.7.0, the @Library was added in 4.7.1 (easy upgrade).

    Your navigation in 4.7.1 should look a bit more like this (note the comment I added there, if a node has no name then you've somehow found a node that hasn't been published yet. this shouldn't happen if you do a foreach on the childeren, but it COULD happen if you do something like Model.NodeById(973974947941)):

    @{ var home = Model.AncestorOrSelf(); }
      <ul>
      <li><a href="@home.Url">Home</a></li>
      @foreach (var item in home.Children)
      {
          if (@item.CurrentTemplate != null)
          {
              var childName = item.Name ?? "(No name yet)"; // NOT TRUE, this means the node is not published!
              <li class=""><a href="@item.Url">@childName </a></li>
          }
      }
      </ul>
  • Kim Andersen 1447 posts 2196 karma points MVP
    Nov 18, 2011 @ 11:11
    Kim Andersen
    0

    Hi again Sebastiaan.

    Nooo man, my opening post isn't confusing as I am using v5 beta 1 in the above question, just as described :) I can make this work in v4.7.1 though without any problems, so no problems there. But my question was regarding v5 :)

    I just downloaded the v5 beta 1 and ran the site through WebMatrix. But I have no clue why the Library gives me an error back. Maybe this stuff isn't implemented yet or it could be a bug or something like that?

    The code that i provided at first in my opening post was just copied from the default navigation.cshtml.

    But great tip on trying to do it the MVC way. I think I will have a look at that. Thanks :)

    /Kim A

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Nov 18, 2011 @ 11:13
    Sebastiaan Janssen
    0

    Right, I see. Anything that's not in the DevDataSet templates is not (properly) implemented yet. And even the stuff in the DevDataSet is still subject to change, so I wouldn't rely on it too much mate. There is navigation in there, so maybe look at that example instead. But I would recommend not to start memorizing the way it's done in there as there's plenty of changes coming there.

  • Rasmus Trumf 78 posts 160 karma points
    Mar 17, 2012 @ 02:30
    Rasmus Trumf
    0

    Im v5  and this is just frustrating. i'm trying my first site now. does that meen it is shit in a month or two ?

Please Sign in or register to post replies

Write your reply to:

Draft