Problems setting an "active" css to current page in nav
I've tried all the solutions I've found in other threads to accomplish this and no luck at all. I think I'm getting further from the solution. I've reverted back to my initial simple nav for now with no active or current css style. Can anyone point me in the right direction?
So far this is my MVC partial View being called onto a page:
Hi Dan,
Thanks so much for the reply. I found a couple different iterations of your suggestion and gave them a try, as well as yours.
It gives me this error on line 11, not sure what I'm missing.
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 9:
Line 10: @foreach (var navPage in pageNode.Children.Where("Visible"))
Line 11: {
Line 12:
You could put an if statement in above the foreach loop to make sure that pageNode is a valid object and not just a null value. Alternatively instead of using Umbraco.Content() try using Umbraco.TypedContent() - the latter gives you a concrete IPublishedContent instance, while the former gives you a dynamic version of it. The concrete version will be much easier to test for this kind of thing.
Also, what's the relationship between the hardcoded pageId and the site? Is it possible to arrive at the same content node using the Ancestor/Descendant methods on the Umbraco instance or one of the other methods? You may find it a lot more stable and less prone to breakage if you can do away with hardcoded Node Ids...
Problems setting an "active" css to current page in nav
I've tried all the solutions I've found in other threads to accomplish this and no luck at all. I think I'm getting further from the solution. I've reverted back to my initial simple nav for now with no active or current css style. Can anyone point me in the right direction?
So far this is my MVC partial View being called onto a page:
I'm calling it on a page using the following:
Hi Brian,
How about this?
Hi Dan, Thanks so much for the reply. I found a couple different iterations of your suggestion and gave them a try, as well as yours.
It gives me this error on line 11, not sure what I'm missing.
Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 9:
Line 10: @foreach (var navPage in pageNode.Children.Where("Visible")) Line 11: { Line 12:- @navPage.pageTitle
Line 13: }
It suggests pageNode hasn't been set properly, perhaps as your ViewData doesn't contain the startNodeId as expected?
Andy
Not sure I am following you, how would I check that?
You could put an if statement in above the foreach loop to make sure that pageNode is a valid object and not just a null value. Alternatively instead of using Umbraco.Content() try using Umbraco.TypedContent() - the latter gives you a concrete IPublishedContent instance, while the former gives you a dynamic version of it. The concrete version will be much easier to test for this kind of thing.
Also, what's the relationship between the hardcoded pageId and the site? Is it possible to arrive at the same content node using the Ancestor/Descendant methods on the Umbraco instance or one of the other methods? You may find it a lot more stable and less prone to breakage if you can do away with hardcoded Node Ids...
- Rob.
Heres how I did it:
Top of my code:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = null;
var active = "active";
var currPageName = Umbraco.Field("pageName").ToString();
}
Navbar:
<ul class="nav navbar-nav">
<li class="@if(currPageName == "Home"){@active}"}>
<a href="/home" class="">
Home
</a>
</li>
</ul>
Probably not the cleanest but maybe you can use the if statement inside your foreach loop?
Kind regards.
is working on a reply...