I'm setup a dynamic navigation menu. Single items work fine however I'm just wondering if someone can point me in the direction of how I can setup dynamic drop down items within the nav?
Here is my code which I used for the dynamic nav bar.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@{
var homePage = Umbraco.TypedContentAtRoot().FirstOrDefault();
var menuPages = homePage.Children().Where(f=>f.IsVisible());
var isHomePage = homePage.Id == Model.Content.Id;
}
<div data-collapse="medium" data-animation="default" data-duration="400" class="navbar w-nav">
<div class="nav-container w-container">
<nav role="navigation" class="nav-menu w-nav-menu">
<a href="@homePage.Url" class="@(isHomePage ? "active" : string.Empty) nav-link w-nav-link">@homePage.Name</a>
@foreach (var item in menuPages)
{
//set active class if current page is in this section
var activeClass = !isHomePage && Model.Content.Path.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Contains(item.Id.ToString()) ? "active" : string.Empty;
<a href="@item.Url" class="@activeClass nav-link w-nav-link">@item.Name</a>
}
</nav>
<div class="menu-button w-nav-button">
<div class="mobile-icon w-icon-nav-menu"></div>
</div>
</div>
</div>
What I'm trying to achieve is the following format;
Unfortunately the organisation this is being designed for are quite strict on allowing 3rd party applications/extensions, and where possible they try to avoid using them.
If its going to be quite complex to do I can put the extension forward and see what they say.
What version of Umbraco are you currently running?
You could do this using a Nested Content property editor (which is part of Umbraco as of, I think, 7.7). You would take a very similar approach to what Michaël has suggested, except you don't need to create a content node in your content tree. Instead you build a Menu Builder data type and add it as a property on your Root Node.
This also gives you the opportunity to have multiple menu item types such as "Menu Label" and "Menu Item", where the Item has a link picker (incase you wanted to link externally) and the Label just has a text field. Both the Label and the Item could inherit from a "Menu item" that allows for a sub-menu to be added so you can perform your nesting.
There isn't a tutorial but the following is a rough guide that I hope can help you get started.
Step 1 - Creating initial document types
I would create a folder called Nested content in the document types section in Umbraco (found in Settings).
In this folder I could create 3 document types (without templates). Menu Item Base , Menu Item Label, and Menu Item Link. On Menu Item Label and Menu Item Link add a tab called Content and a property called Label (or something similar).
Set both Menu Item Label and Menu Item Link to have a composition of Menu Item Base
Step 2 - Create the nested content based data type
Goto the Data Types node in the Developer section of Umbraco and create a folder called "Nested Content" (You don't have I just think it is better to group things together).
Create a new Data Type, I'd call it Menu Builder. From the drop down of property editors, pick Nested Content.
Add two document types to the options, these document types are our Menu Item Label and Menu Item Link. For each select the Content tab and in the label field add {{label}} (assuming you called the property label).
Step 3 - Finish off your document types
So, here we want to edit our Menu Item Base document type, add a tab called "Content" (same name as the tab on both the Menu Item Link and Menu Item Label document types. Add a property called "Submenu" of type "Menu Builder" (Our nested content menu builder type). Personally, I would reorder properties here so I can give the Submenu property a sort value of 10 to keep it at the bottom.
The Menu Item Link needs to have a property added to it that allows you to add a link, be it a link picker for internal links or something else. That decision is up to you.
So, at this point you should have 3 new document types that have a structure that appears similar to this:
Menu Item Base - Content tab - Submenu Property
Menu Item Label - Content tab - Label Property, Submenu Property
Menu Item Link - content tab - Label Property, Link Property, Submenu Property
Step 4) (I'm going to speed up a bit here now)
Add a property to your Home Page of type Menu Builder. You might want to add a new tab for it but that is up to you.
At this point, you can now build your menu in the back office.
All that is left now is rendering out you menu, I say all, it will take a bit of time but effectively a property that is of type Nested Content will store it's information as an IEnumerable<IPublishedContent> so, assuming you've called your Menu property 'Menu' you can access it like this:
var menuEntries = Model.Content.Site().GetPropertyValue<IEnumerable,<IPublishedContent>>("menu");
You can then look around them as follows:
foreach(var menuItem in menuEntries)
{
//do something here, check if this is a label or a link as you might want to render it out differently.
//Now handle the submenu
var subMenu = menuItem.GetPropertyValue<IEnumerable<IPublishedContent>>();
if(subMenu != null && subMenu.Any()) //Checking we have entries
{
//In here you would handle the sub menu items.
//Realistically, you would probably have some sort of
// reccursive function call to handle this so depth of nested menus doesn't matter.
}
}
Did you install it via NuGet or the Package Manager in the back office?
I do find that the back office doesn't always show up new editors, so you might be getting an annoying caching issue. If you haven't, it could be worth restarting the website and seeing if it turns up then.
No problem, what version of .Net is your project set up for? I've had an issue where I was set up as .Net 4.5, but needed to be 4.6 to get MegaNav to work. If you are currently 4.5, update your project to 4.6.1 and re-install MegaNav
I'm still trying to get my head around how Meganav works. I followed the screenshots in the document and set my new data type up.
I have a couple of questions;
1) Do I need to add and set Meganav datatype on each document type I create? or just on the Home as when I set a few test pages up it looks nothing like the documentation when I'm looking to create something like;
Home
About
About us
What we do
Blog
Contact
2) When I use the "ExampleNavigation.cshtml" which comes with Meganav I get the following error So I cant actually see how anything looks or works.
The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel`1[Umbraco.Web.PublishedContentModels.Home]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Cogworks.Meganav.Models.MeganavItem]'.
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.InvalidOperationException: The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel`1[Umbraco.Web.PublishedContentModels.Home]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Cogworks.Meganav.Models.MeganavItem]'.
Okay, so if you are only having 1 navigation on your site, then you only need to add it to your root node. If you were displaying some sort of secondary menu on certain pages you might look at adding another one somewhere else for that.
When you add nodes to the mega nav, you can move them in 4 directions.
Up
Down,
left,
and Right.
Up and down sorts their position, left and right sorts their level.
so, I would create 4 nodes initially, Home, About, Blog, and Contact.
Then, I would add About us, drag this up to beneath About and right one notch. It can be a little fiddly so just persevere with it. Repeat this for the What we do node.
With regards to rendering it out, it looks like you are using Models Builder, which is great.
So I'm going to assume that you've called your menu property "Menu".
So to render the menu using the example partial use the following line of code:
This should work because MegaNav comes with the correct property converter so Models Builder knows that the property is of type IEnumerable<MeganavItem>
Thanks once I get the menu to output I shall have a play around on the test environment and see how things work out.
I'm still getting an error tho;
Compiler Error Message: CS1061: 'Umbraco.Web.PublishedContentModels.Home' does not contain a definition for 'Menu' and no extension method 'Menu' accepting a first argument of type 'Umbraco.Web.PublishedContentModels.Home' could be found (are you missing a using directive or an assembly reference?)
I assume this is because of "Menu" when you say my menu property what do you mean?
That's no problem, happy to help. So, when you've added a property to your home Document Type, (of type MegaNav) you've given it a name. When I've said "Menu" I've assumed that is what you've called the property. If you've called the property MegaNav, then replace Menu with MegaNav, etc.
So, generally the contents of a navigation are structured like this:
<ul>
<li><a>My first entry</a></li>
<li><a>My second entry</a></li>
<ul>
The reason for this is so that when you need to do a drop down you can look to do something like this:
<ul>
<li><a>My first entry</a></li>
<li>
<a>My second entry</a>
<ul class="subMenu">
<li><a>My sub menu item</a></li>
<li><a>My second sub menu item</a></li>
</ul>
</li>
<ul>
This allows you to have you association between each level of the menu. From this perspective, there are hundreds of tutorials about this sort of structure online, along with css snippets etc so rather than me try and provide something for you, it would be worth having a look around when it comes to that.
You'll want to update your razor logic (particularly focussing on your helper) and think about how that renders out, for example, one option might be to adjust it so it's like this:
This is my complete HTML menu which works fine outside of meganav and works for single menu items. Is there no way to use this sort of menu structure with meganav?
I've never seen that sort of navigation structure and it's not one I would choose to use, I'm sure you could find a way but you'd probably need some javascript to help it.
Maybe someone else could help with that but it's more a front end problem now rather than an Umbraco specific issue :-)
Also I tried installing another test environment and I'm getting the following error again;
Compiler Error Message: CS1061: 'Umbraco.Web.PublishedContentModels.Home' does not contain a definition for 'mainNavigation' and no extension method 'mainNavigation' accepting a first argument of type 'Umbraco.Web.PublishedContentModels.Home' could be found (are you missing a using directive or an assembly reference?)
I've checked my document type and "mainNavigation" is there...
Soon as I get this menu resolved I'm going to create a tutorial so others don't suffer with the same headache as me!
Need someone help. I'm implementing the meganav but different errors occurred. I have read the discussion of Nik and Matt and try this but failed with an error.
Error is "Object reference not set to an instance of an object."
@{
var homeNode = Model.Content.Site().OfType<Menus>();
@Html.Partial("ExampleNavigation", homeNode.MenuBar)
}
dynamic drop down menu
Hello all,
I'm setup a dynamic navigation menu. Single items work fine however I'm just wondering if someone can point me in the direction of how I can setup dynamic drop down items within the nav?
Here is my code which I used for the dynamic nav bar.
What I'm trying to achieve is the following format;
Nav Item
ect ect
Hi Matt,
have a look at this package:
https://our.umbraco.org/projects/website-utilities/meganav/
Maybe it can be the solution you are looking for.
Have a nice day!
/Michaël
Hello Michael,
Thanks for the quick reply, do you know how its possible to achieve the following without using a 3rd party extension?
Matt
Hi Matt,
this is possible, but then we will need to know your way of how your content structure is build in the backoffice and how you want it to become.
Can you give us more details about this or even screenshots?
Thanks!
/Michaël
Hello Michael,
I've attached how my content looks.
For e.g I would like my menu dynamic menu to look like;
Hi Matt,
the meganav package is really usefull for this kind of tasks, what's the reason not to use 3th party tools?
/Michaël
Hello Michael,
Unfortunately the organisation this is being designed for are quite strict on allowing 3rd party applications/extensions, and where possible they try to avoid using them.
If its going to be quite complex to do I can put the extension forward and see what they say.
Is not an easy task?
Matt
Hi Matt,
You can do it yourself, but the way you describe makes me wondering why spent time on it when there is a package for it?
If you really want to make it yourself you need to find a way of making it transparent for other users.
Menu
doc type with a content picker ( which allows you to pick a content node from your content section )Menu
doc type to allow children of the sameMenu
doc typeMenu Structure
which contains the menu items structureMenu Structure
nodeHope this helps!
/Michaël
Hi Matt,
What version of Umbraco are you currently running? You could do this using a Nested Content property editor (which is part of Umbraco as of, I think, 7.7). You would take a very similar approach to what Michaël has suggested, except you don't need to create a content node in your content tree. Instead you build a Menu Builder data type and add it as a property on your Root Node.
This also gives you the opportunity to have multiple menu item types such as "Menu Label" and "Menu Item", where the Item has a link picker (incase you wanted to link externally) and the Label just has a text field. Both the Label and the Item could inherit from a "Menu item" that allows for a sub-menu to be added so you can perform your nesting.
Just another idea :-)
Nik
Hello Nik,
I'm currently using version 7.9.2
Is there a tutorial around to do such method?
Matt
Hi Matt,
There isn't a tutorial but the following is a rough guide that I hope can help you get started.
Step 1 - Creating initial document types
Menu Item Base
,Menu Item Label
, andMenu Item Link
. OnMenu Item Label
andMenu Item Link
add a tab calledContent
and a property calledLabel
(or something similar).Menu Item Label
andMenu Item Link
to have a composition ofMenu Item Base
Step 2 - Create the nested content based data type
Nested Content
.Menu Item Label
andMenu Item Link
. For each select theContent
tab and in the label field add{{label}}
(assuming you called the property label).Step 3 - Finish off your document types
So, here we want to edit our
Menu Item Base
document type, add a tab called "Content" (same name as the tab on both the Menu Item Link and Menu Item Label document types. Add a property called "Submenu" of type "Menu Builder" (Our nested content menu builder type). Personally, I would reorder properties here so I can give the Submenu property a sort value of 10 to keep it at the bottom.The Menu Item Link needs to have a property added to it that allows you to add a link, be it a link picker for internal links or something else. That decision is up to you.
So, at this point you should have 3 new document types that have a structure that appears similar to this:
Menu Item Base - Content tab - Submenu Property Menu Item Label - Content tab - Label Property, Submenu Property Menu Item Link - content tab - Label Property, Link Property, Submenu Property
Step 4) (I'm going to speed up a bit here now) Add a property to your Home Page of type Menu Builder. You might want to add a new tab for it but that is up to you.
At this point, you can now build your menu in the back office.
All that is left now is rendering out you menu, I say all, it will take a bit of time but effectively a property that is of type Nested Content will store it's information as an
IEnumerable<IPublishedContent>
so, assuming you've called your Menu property 'Menu' you can access it like this:You can then look around them as follows:
I hope that this helps you :-)
Nik
Hello Nik,
thanks for that detailed explaination, I've installed a test copy of umbraco and will trial these suggested options.
I've tried to install the Meganav extention which says everything has installed fine.
However when I go to the next step of the usage in the documentation i don't see Meganav Data Type? I'm on version 7.9.2
Hi Matt,
Did you install it via NuGet or the Package Manager in the back office? I do find that the back office doesn't always show up new editors, so you might be getting an annoying caching issue. If you haven't, it could be worth restarting the website and seeing if it turns up then.
Thanks,
Nik
Hello Nik,
Thanks for the quick reply, I installed via Nugget and tried multiple fresh installs/versions but still no joy.
Matt
Hey Matt,
No problem, what version of .Net is your project set up for? I've had an issue where I was set up as .Net 4.5, but needed to be 4.6 to get MegaNav to work. If you are currently 4.5, update your project to 4.6.1 and re-install MegaNav
I like the ignoreDependecies tag in that command as it means it won't try and re-install loads of things that aren't needed.
Nik
Hello Nik,
Perfect that worked a dream, I was running 4.5
I'm still trying to get my head around how Meganav works. I followed the screenshots in the document and set my new data type up.
I have a couple of questions;
1) Do I need to add and set Meganav datatype on each document type I create? or just on the Home as when I set a few test pages up it looks nothing like the documentation when I'm looking to create something like;
2) When I use the "ExampleNavigation.cshtml" which comes with Meganav I get the following error So I cant actually see how anything looks or works.
Thanks in advance,
Matt
Hi Matt,
Okay, so if you are only having 1 navigation on your site, then you only need to add it to your root node. If you were displaying some sort of secondary menu on certain pages you might look at adding another one somewhere else for that.
When you add nodes to the mega nav, you can move them in 4 directions.
Up Down, left, and Right.
Up and down sorts their position, left and right sorts their level.
so, I would create 4 nodes initially, Home, About, Blog, and Contact. Then, I would add About us, drag this up to beneath About and right one notch. It can be a little fiddly so just persevere with it. Repeat this for the What we do node.
With regards to rendering it out, it looks like you are using Models Builder, which is great.
So I'm going to assume that you've called your menu property "Menu".
So to render the menu using the example partial use the following line of code:
This should work because MegaNav comes with the correct property converter so Models Builder knows that the property is of type
IEnumerable<MeganavItem>
Let me know how you get on.
Nik
Hello Nik,
Thanks once I get the menu to output I shall have a play around on the test environment and see how things work out.
I'm still getting an error tho;
I assume this is because of "Menu" when you say my menu property what do you mean?
Thanks in advance Nik, really appreciate it
Matt
Hi Matt,
That's no problem, happy to help. So, when you've added a property to your home Document Type, (of type MegaNav) you've given it a name. When I've said "Menu" I've assumed that is what you've called the property. If you've called the property MegaNav, then replace Menu with MegaNav, etc.
That make sense?
Thanks,
Nik
Hello Nik,
Thanks that worked a treat I've added my CSS to the code and everything works fine for non child navigation.
My next issue if your able to help is how can I get
To only display on nodes with children when you hover over them?
Here is my CSS style which I used for drop down navs.
And here is my partial view
Thanks
Matt
Hi Matt,
So, generally the contents of a navigation are structured like this:
The reason for this is so that when you need to do a drop down you can look to do something like this:
This allows you to have you association between each level of the menu. From this perspective, there are hundreds of tutorials about this sort of structure online, along with css snippets etc so rather than me try and provide something for you, it would be worth having a look around when it comes to that.
You'll want to update your razor logic (particularly focussing on your helper) and think about how that renders out, for example, one option might be to adjust it so it's like this:
Thanks,
Nik
Hello Nik,
Thanks for this response, can I only achieve the drop down method if I use the list method?
This is my complete HTML menu which works fine outside of meganav and works for single menu items. Is there no way to use this sort of menu structure with meganav?
Really appreciate your help.
Matt
Hi Matt,
I've never seen that sort of navigation structure and it's not one I would choose to use, I'm sure you could find a way but you'd probably need some javascript to help it.
Maybe someone else could help with that but it's more a front end problem now rather than an Umbraco specific issue :-)
Nik
Thanks for the help Nik.
Anyone else here have any ideas how I can use the above nav with MegaNav? I have all the html/css just need help with adding meganav around it.
Matt
Also I tried installing another test environment and I'm getting the following error again;
I've checked my document type and "mainNavigation" is there...
Soon as I get this menu resolved I'm going to create a tutorial so others don't suffer with the same headache as me!
Can you share tutorial link here??
Need someone help. I'm implementing the meganav but different errors occurred. I have read the discussion of Nik and Matt and try this but failed with an error.
Error is "Object reference not set to an instance of an object."
is working on a reply...