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:
"Compiler Error Message: CS0234: The type or namespace name 'MacroEngines' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)"
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 :))
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.
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>
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 :)
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.
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:
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:
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:
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
Hi Kim,
Just a shot in the dark, but could it be a missing reference to: umbraco.MacroEngines.Library ? :-)
Hi Bo
Just tried inserting this:
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
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 :))
Yes sir, I will for sure :)
One way of doing it in v5 beta1:
A bit clumsy though, should be a way to write it in-line :)
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
There is:
How about them apples!
For the record, this is something called a ternary operator.
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
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.
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)):
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
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.
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 ?
is working on a reply...