d:\web\localuser\mydomain\public_html\macroScripts\634841164413937208_FitnessMainMenu.cshtml(18): error CS0103: The name 'selectedClass' does not exist in the current context
Still getting the same error, that it doesnt exist in the current context:
Error occured
d:\web\localuser\penge-portal.dk\public_html\macroScripts\634841170896372593_FitnessMainMenu.cshtml(17): error CS0103: The name 'selectedClass' does not exist in the current context
Very sorry my bad.....was doing something and didnt notice i added some silly errors in this threat. I was actually using a dictionary item to the var homeNode.
So instead it should be this
@inherits umbraco.MacroEngines.DynamicNodeContext @{ var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.StartLevel); var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 5 : int.Parse(Parameter.FinishLevel); var parent = @Model.AncestorOrSelf(startLevel); if (parent != null) { @traverse(parent,startLevel,finishLevel) ; }
<li@library and <li@Html look a bit suspect, but Umbraco 6 is strict Razor 2, and if you have an @ where it shouldn't be it breaks code, so if it isn't your li's it might be the &&@node.Level , you could try removing the @ and see if it runs.
No, can't get it to save at all in the Umbraco UI. And in VS2012 can't get Razor syntax checking. It insists on doing html5 checking on the cshtml file! Ahhh! Very frustrating. I just want a normal navigation macro that starts at the home page rather than one level down, which is what the pre-built example does for some odd reason. You'd think the pre-built example would cover the whole thing rather than leave you without a home page!
Not completely sure I understand, but if you block out the code section by section you will find where your error is, ie get it to save and then work on the part that causes it not to save.
You have a level parameter for startLevel which is currently set to 1, so any children of that will be level 2, changing this parameter to 0, should give you the homepage if you have the "regular" Umbraco tree set-up.
The error on saving is "c:\Users\Craig\AppData\Local\Temp\Temporary ASP.NET Files\root\9c9cf0f0\1b14c0a9\App_Web_634984379898353587_mainmenu.cshtml.70df5e80.5pgeb38h.0.cs(91): error CS1513: } expected"
Doesn't look like it needs an extra "}" anywhere to me. Don't know if there's something about Razor I'm just not getting or if there's a real issue. Wasting a lot of time with Razor syntax lately.
Have had similar issues myself, but this is not Umbraco, its the mvc bit with razor syntax. There is a post on it somewhere, but I cant find it at the moment.
var parent = Model.AncestorOrSelf - shouldn't need the @, this used to work, but takes it out of code, so hence you have no } to close.
@{ var startLevel =String.IsNullOrEmpty(Parameter.Level)?0:int.Parse(Parameter.StartLevel); var finishLevel =String.IsNullOrEmpty(Parameter.Level)?1:int.Parse(Parameter.FinishLevel); var parent =Model.AncestorOrSelf(startLevel);
Without the whole code, thats the best I can see, it appears your if statement goes nowhere, ie its closed too early. I have taken to calling the node vars in a separate code block at the top, then starting a new codeblock for any other statement.
Thanks, got it to save now. But it ouputs nothing at all. All I have is a home page and an about page:-
Home ----About
So set startLevel at 0 and finishLevel at 1.
Another oddity is you can't add a variable inside an html tag properly without leaving an unnecessary space. i.e. <li @isHomeSelected> if isHomeSelected is nothing will give <li >. If there is no space, as you would want, to give <li>..... <li@isHomeSelected> then it won't save.
The final code I have is:-
@inherits umbraco.MacroEngines.DynamicNodeContext @{ var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 0 : int.Parse(Parameter.StartLevel); var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.FinishLevel); var parent = Model.AncestorOrSelf(startLevel); if (parent != null) {traverse(parent,startLevel,finishLevel);} }
If there's anything obvious as to why it won't work it would be great to hear it. Razor is getting to be unusable for me as it's taking too long to get stuff done.
Because you call parent.children, it will only output About.
.Where("includeInMainNav == true") if this is a yes/no, I belive it outputs a zero or one, so won't return for true. The usual way is to have a yes/no property on the docType with an alias of umbracoNaviHide, then you can use the .Where("Visible").
Still trying to find a documant on razor V2, sure it would clear up some of these niggles. But XSLT - - no chance.
I gave up with this in the end and re-engineered it from a few other posts and documents.
includeInMainNav is a true/false field in the docType. In this site there are more pages hidden than not as a lot of them are page parts, so this logic was more expedient.
This now compiles and works nicely. Hope it helps someone else:-
@inherits umbraco.MacroEngines.DynamicNodeContext <ul> @{ var homeNode = Model.AncestorOrSelf("Home"); //Sets the start point var homeActive = Library.If(Model.NodeTypeAlias == "Home", "menuLeftDivActive", ""); <li class="@homeActive"><a href="/" title="Home">Home</a></li> foreach (var page in homeNode.Children.Where("includeInMainNav == true")) { var pageActive = ""; if (Array.IndexOf(Model.Path.Split(','), page.Id.ToString()) >= 0) { pageActive = "menuLeftDivActive"; } pageActive += page.IsLast(" menuLeftDivLast",""); <li class="@pageActive.Trim()"><a href="@page.Url" title="@page.name">@page.name</a></li> } } </ul>
Must say, that code does look a lot cleaner and readable, I did think of suggesting something else but was fearful it would confuse more than it solved.
Still haven't found some decent on Razor V2 MVC4, but came across this link that might interest you regarding <li> classes with razor and nulls.
AncestorOfSelf in navigation?
Hey Guys...
Im having problems getting my HomePage (topnode) shown in my navigation...
My sitetree looks like
- Home
- Blog
- About
- Contact
However, my navigation only ouputs: Blog, About and Contact, It should Output "Home" aswell?
@* RAZOR *@
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.StartLevel);
var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 5 : int.Parse(Parameter.FinishLevel);
var parent = @Model.AncestorOrSelf(startLevel);
if (parent != null) { @traverse(parent,startLevel,finishLevel) ; }
}
@helper traverse(dynamic parent,int startLevel,int finishLevel)
{
<ul>
@foreach (var node in parent.Children.Where("Visible"))
{
var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
<li@Html.Raw(selected)>
<a href="@node.Url">@node.Name</a>
@if (selected!=""&&@node.Level<=finishLevel) { @traverse(node,startLevel,finishLevel); }
</li>
}
</ul>
}
Hi Nicky ,
If you want your top node "Homepage" to display as part of your navigation you could try something like
Before your foreach loop
//fuji
Hmm, then I just get an error?
Error occured
d:\web\localuser\mydomain\public_html\macroScripts\634841164413937208_FitnessMainMenu.cshtml(18): error CS0103: The name 'selectedClass' does not exist in the current context
The whole code is:
@* RAZOR *@
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.StartLevel);
var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 5 : int.Parse(Parameter.FinishLevel);
var parent = @Model.AncestorOrSelf(startLevel);
if (parent != null) { @traverse(parent,startLevel,finishLevel) ; }
}
@helper traverse(dynamic parent,int startLevel,int finishLevel)
{
<ul>
const string selectedClass = " class=\"selected\"";
var homeNode = Home;
<li@Library.If(Model.NodeTypeAlias == "FitnessFrontPage", selectedClass)><a href="/">@homeNode</a></li>
@foreach (var node in parent.Children.Where("Visible"))
{
var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
<li@Html.Raw(selected)>
<a href="@node.Url">@node.Name</a>
@if (selected!=""&&@node.Level<=finishLevel) { @traverse(node,startLevel,finishLevel); }
</li>
}
</ul>
}
Here
Still getting the same error, that it doesnt exist in the current context:
Error occured
d:\web\localuser\penge-portal.dk\public_html\macroScripts\634841170896372593_FitnessMainMenu.cshtml(17): error CS0103: The name 'selectedClass' does not exist in the current context
@* RAZOR *@
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.StartLevel);
var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 5 : int.Parse(Parameter.FinishLevel);
var parent = @Model.AncestorOrSelf(startLevel);
if (parent != null) { @traverse(parent,startLevel,finishLevel) ; }
const string selectedClass = " class=\"selected\"";
var homeNode = Home;
}
@helper traverse(dynamic parent,int startLevel,int finishLevel)
{
<ul>
<li@Library.If(Model.NodeTypeAlias == "FitnessFrontpage", selectedClass)><a href="/">@homeNode</a></li>
@foreach (var node in parent.Children.Where("Visible"))
{
var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " class=\"selected\"" : "";
<li@Html.Raw(selected)>
<a href="@node.Url">@node.Name</a>
@if (selected!=""&&@node.Level<=finishLevel) { @traverse(node,startLevel,finishLevel); }
</li>
}
</ul>
}
Nicky,
Very sorry my bad.....was doing something and didnt notice i added some silly errors in this threat. I was actually using a dictionary item to the var homeNode.
So instead it should be this
Ahhh great, thx alot mate, works perfectly :)
This looked promising but in Umb 6.02 this gives: Encountered end tag "ul" with no matching start tag. Are your start/end tags properly balanced?
What's the best way to fix it?
Craig
Hi Craig
<li@library and <li@Html look a bit suspect, but Umbraco 6 is strict Razor 2, and if you have an @ where it shouldn't be it breaks code, so if it isn't your li's it might be the &&@node.Level , you could try removing the @ and see if it runs.
No, can't get it to save at all in the Umbraco UI. And in VS2012 can't get Razor syntax checking. It insists on doing html5 checking on the cshtml file! Ahhh! Very frustrating. I just want a normal navigation macro that starts at the home page rather than one level down, which is what the pre-built example does for some odd reason. You'd think the pre-built example would cover the whole thing rather than leave you without a home page!
Hi
Not completely sure I understand, but if you block out the code section by section you will find where your error is, ie get it to save and then work on the part that causes it not to save.
You have a level parameter for startLevel which is currently set to 1, so any children of that will be level 2, changing this parameter to 0, should give you the homepage if you have the "regular" Umbraco tree set-up.
If I can help some more, please let me know.
G
Hi,
It's not that it won't run, it won't even save in the Umbraco UI.
Even if I strip it back to the following where the ul tags are clearly "in balance"
The error on saving is "c:\Users\Craig\AppData\Local\Temp\Temporary ASP.NET Files\root\9c9cf0f0\1b14c0a9\App_Web_634984379898353587_mainmenu.cshtml.70df5e80.5pgeb38h.0.cs(91): error CS1513: } expected"
Doesn't look like it needs an extra "}" anywhere to me. Don't know if there's something about Razor I'm just not getting or if there's a real issue. Wasting a lot of time with Razor syntax lately.
Craig
Hi Craig
Have had similar issues myself, but this is not Umbraco, its the mvc bit with razor syntax. There is a post on it somewhere, but I cant find it at the moment.
var parent = Model.AncestorOrSelf - shouldn't need the @, this used to work, but takes it out of code, so hence you have no } to close.
}
}
}
Without the whole code, thats the best I can see, it appears your if statement goes nowhere, ie its closed too early. I have taken to calling the node vars in a separate code block at the top, then starting a new codeblock for any other statement.
I don't think you need the '@' sign before traverse in your if statement, as you're already in c# mode
Cheers,
/Dirk
Thanks, got it to save now. But it ouputs nothing at all. All I have is a home page and an about page:-
Home
----About
So set startLevel at 0 and finishLevel at 1.
Another oddity is you can't add a variable inside an html tag properly without leaving an unnecessary space. i.e. <li @isHomeSelected> if isHomeSelected is nothing will give <li >. If there is no space, as you would want, to give <li>..... <li@isHomeSelected> then it won't save.
The final code I have is:-
Hi Craig
Glad it saves at least. The space is a new "rule", but I couldn't bring it to mind why the <li@ looked supspect, but you have it done now.
Ok, if you have nothing above Home, it should only output, About, it is important to have a root node if you need to use the tree in this way. This blog post from Sebaastian is still relevant. http://cultiv.nl/blog/2010/12/19/tip-of-the-week-the-ultimate-site-structure-setup/
Because you call parent.children, it will only output About.
.Where("includeInMainNav == true") if this is a yes/no, I belive it outputs a zero or one, so won't return for true. The usual way is to have a yes/no property on the docType with an alias of umbracoNaviHide, then you can use the .Where("Visible").
Still trying to find a documant on razor V2, sure it would clear up some of these niggles. But XSLT - - no chance.
G
Thanks Gary,
I gave up with this in the end and re-engineered it from a few other posts and documents.
includeInMainNav is a true/false field in the docType. In this site there are more pages hidden than not as a lot of them are page parts, so this logic was more expedient.
This now compiles and works nicely. Hope it helps someone else:-
Craig
Nice One!
Must say, that code does look a lot cleaner and readable, I did think of suggesting something else but was fearful it would confuse more than it solved.
Still haven't found some decent on Razor V2 MVC4, but came across this link that might interest you regarding <li> classes with razor and nulls.
http://stackoverflow.com/questions/12694814/element-not-closed-error-after-upgrading-from-mvc3-to-mvc4
@Craig100 - That is a great solution to the problem and it helped me out big time!! Thanks
is working on a reply...