I'm using MegaNav but when I try to render the partial view I get the following error.
> Compiler Error Message: CS1061:
> 'Umbraco.Core.Models.IPublishedContent' does not contain a definition
> for 'mainNavigation' and no extension method 'mainNavigation'
> accepting a first argument of type
> 'Umbraco.Core.Models.IPublishedContent' could be found (are you
> missing a using directive or an assembly reference?)
This is how I try to render the my partial view in my template;
Do you have Models Builder enabled in your website?
If so, Model.Content should be a typed model, what this means is that it should have a property on it called MainNavigation not mainNavigation, note the capital M at the start.
Something to consider though, is that this approach won't work when you are trying to render the menu from a root/home node on subpages. So we might need to address that, but in the mean time lets get your menu rendering.
Okay, so the quickest way to see if ModelsBuilder is enabled is look in your webconfig. In App Settings you should see an element called (I think) Models.Builder.Mode, by default it is set to PureLive.
@{
var homeNode = Model.Content.Site().OfType<Home>();
@Html.Partial("ExampleNavigation", homeNode.MainNavigation)
}
Basically, your master layout doesn't know the type of your current page so using the direct property access doesn't work.
However, what you can do is get your root node from your current node by using .Site(). You can then use the .OfType extension method to convert the IPublishedContent it returns to the strongly typed model from Models Builder for your Home node. Then you can access the MainNavigation property.
Doing this will also make it work for sub pages as well.
Thanks that makes a little more sense why it isn't working.
I tried adding your code to my master template but still getting the same error;
Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'MainNavigation' and no extension method 'MainNavigation' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
No problem, can I see your entire master layout? and also the code for the navigation partial? (Just incase Razor is being annoying and saying the error is somewhere it isn't)
So, what should a link that doesn't have children look like compared to a link that does have children? Can you provide snippets of the html you are expecting?
Matt, could you create a plain HTML version of what you want your full navigation to look like? With the correct classes in place and examples of no children and multiple children?
So what you want your HTML to look like if your structure was this:
There is probably some code errors in this but, this is roughly what I would do:
@using Cogworks.Meganav.Models
@model IEnumerable<MeganavItem>
<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">
//The top level requires different rendering to children so let us split it up.
@foreach(var topLevelEntry in Model)
{
//We need to check if this has children or not
if(topLevelEntry.Children != null && topLevelEntry.Children.Any())
{
<div data-hover="1" data-delay="0" class="drop-down-menu w-dropdown">
<div class="nav-link w-dropdown-toggle">
<div>@topLevelEntry.Title</div>
</div>
<nav class="w-dropdown-list">
@RenderChildren(topLevelEntry.Children)
</nav>
</div>
}else{
<a href="@topLevelEntry.Url" class="nav-link w-nav-link">@topLevelEntry.Title</a>
}
}
</nav>
<div class="menu-button w-nav-button">
<div class="mobile-icon w-icon-nav-menu"></div>
</div>
</div>
@helper RenderChildren(IEnumerable<MeganavItem> items)
{
if (items.Any())
{
foreach (var item in items)
{
<a href="@item.Url" class="drop-down-link w-dropdown-link">@item.Title</a>
}
}
}
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."
Line 91: @{
Line 92: var homeNode = Model.Content.Site().OfType
@{
var homeNode = Model.Content.AncestorOrSelf(1);
@Html.Partial("ExampleNavigation", homeNode.MenuBar)
}
Still gives compilation error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'MenuBar' and no extension method 'MenuBar' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
No I haven't any model related to meganav or menus documents (doc type).
I have read some other post that are used to make navigation using multinode tree picker but failed..... These navigation class makes thier own models and controllers.
all.generated.cs does not update models definition like I have made Menus.cshtml then I go to all.generated.cs and copy paste the definition of previous model (like category document type) and then change previous model definition to new one (Menus document type).
New definition is:
/// <summary>Menus</summary>
[PublishedContentModel("menus")]
public partial class Menus : PublishedContentModel
{
pragma warning disable 0109 // new is redundant
public new const string ModelTypeAlias = "menus";
public new const PublishedItemType ModelItemType = PublishedItemType.Content;
pragma warning restore 0109
public Menus(IPublishedContent content)
: base(content)
{ }
pragma warning disable 0109 // new is redundant
public new static PublishedContentType GetModelContentType()
{
return PublishedContentType.Get(ModelItemType, ModelTypeAlias);
}
pragma warning restore 0109
public static PublishedPropertyType GetModelPropertyType<TValue>(Expression<Func<Menus, TValue>> selector)
{
return PublishedContentModelUtility.GetModelPropertyType(GetModelContentType(), selector);
}
///<summary>
/// MenuBar
///</summary>
[ImplementPropertyType("menuBar")]
public IEnumerable<Cogworks.Meganav.Models.MeganavItem> MenuBar
{
get { return this.GetPropertyValue<IEnumerable<Cogworks.Meganav.Models.MeganavItem>>("menuBar"); }
}
}
I just want to ask that is there any problem. Same I have done with models.generated.cs.
MegaNav Partial View
Hello all,
I'm using MegaNav but when I try to render the partial view I get the following error.
This is how I try to render the my partial view in my template;
Hi Matt,
Do you have Models Builder enabled in your website? If so, Model.Content should be a typed model, what this means is that it should have a property on it called
MainNavigation
notmainNavigation
, note the capital M at the start.Something to consider though, is that this approach won't work when you are trying to render the menu from a root/home node on subpages. So we might need to address that, but in the mean time lets get your menu rendering.
Nik
Hello Nik,
Thanks for the quick reply, how would I check Models Builder is enabled?
I installed Meganav on my test website which worked fine, that had a captain M at the start but doesn't seem to work on the new website.
Thanks
Okay, so the quickest way to see if ModelsBuilder is enabled is look in your webconfig. In App Settings you should see an element called (I think) Models.Builder.Mode, by default it is set to PureLive.
Nik
Hello Nik,
Here is what I have;
Okay, so looks like you have Models Builder enabled.
Could you provide an image of what your site structure looks like in the content tree?
Here you go Nik, strange how it worked on my test....
Thanks Matt, and to confirm this property:
Main Navigation
is on the Home document type yes?Is your call from your partial view being called from a shared layout file that all the other templates use?
Nik
Hello Nik,
Yeap its in my home document type
Yes it is, The partial view is in my Master template
Thanks Matt
Okay,
Try this code:
Basically, your master layout doesn't know the type of your current page so using the direct property access doesn't work.
However, what you can do is get your root node from your current node by using
.Site()
. You can then use the.OfType
extension method to convert the IPublishedContent it returns to the strongly typed model from Models Builder for your Home node. Then you can access the MainNavigation property. Doing this will also make it work for sub pages as well.Nik
Hello Nik,
Thanks that makes a little more sense why it isn't working.
I tried adding your code to my master template but still getting the same error;
Sorry!
No problem, can I see your entire master layout? and also the code for the navigation partial? (Just incase Razor is being annoying and saying the error is somewhere it isn't)
Hello Nik,
Here is my Master Template;
And here is my ExampleNavigation.cshtml Partial view;
Hi Matt,
In your master template you still have:
Delete that line and replace it with:
Don't put that at the top like you currently have :-)
Nik
Aha!
Working like a dream, I owe you a drink!
Hello Nik,
Would you know why I'm getting this error
When using @item.Child
Hi Matt,
That would be because the MegaNav item model doesn't have a property of method called Child.
You can access Child items by using the Children property.
Nik
Ah ok.
My issue is that I want to style each of the Children;
So as I'm just using @RenderNavigation(item.Children) do you know how could i add a class to those "Render links" ?
Is that your full navigation partial?
Sorry no here it is;
So, what should a link that doesn't have children look like compared to a link that does have children? Can you provide snippets of the html you are expecting?
Hello Nik,
So what I am trying to do is style the nav link;
Here is the html version;
So in theory I need to add the class "drop-down-link w-dropdown-link" to the children.
I thought I could do something like this;
But @item.Children doesn't seem to work.
Matt, could you create a plain HTML version of what you want your full navigation to look like? With the correct classes in place and examples of no children and multiple children?
So what you want your HTML to look like if your structure was this:
Also, how many levels deep would your menu go?
Hello Nik
Here is sample of just HTML with drop downs, I plan to only have 1 level.
Hi Matt,
There is probably some code errors in this but, this is roughly what I would do:
Hello Nik,
Great I shall give it try, Sorry I'm competly new and trying to learn this language.
Should this go at the bottom of the page?
It's not a problem Matt, no need to apologise,
Yep, put that at the bottom of your navigation partial view.
If you have questions about what it's doing, let me know and I'll try and explain it.
Nik
Hello Nik,
That worked a treat, thank you. 2 weeks I been trying to get this working!
I've took up lots of your time up already today, but if you get some spare time it would be great if you can explain it.
Matt
I'll see what I can do, but it won't be today. I'll try and get an explanation written over the weekend for you :-)
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." Line 91: @{ Line 92: var homeNode = Model.Content.Site().OfType
Hi Zubair,
Please ensure the code is
and not
I'm sending you my masterpage code + menus code + ExampleNavigation
Master Page:
@inherits UmbracoTemplatePage
@{ Layout = null; }
@@{ var homeNode = Model.Content.Site().OfType@ @Html.Partial("PVMaster")
Menus
@inherits UmbracoTemplatePage
ExampleNavigation
@using Cogworks.Meganav.Models @model IEnumerable
@RenderNavigation(Model)
@helper RenderNavigation(IEnumerable
@foreach (var item in items) {-
@item.Title (Level: @item.Level)
}
I have changed it little bit and sent you the new code. New Master page code is here:
@inherits UmbracoTemplatePage
@{ Layout = null; }
@{ var homeNode = Model.Content.Site().OfType
@inherits UmbracoTemplatePage
@{ Layout = null; } . . . .
@{ var homeNode = Model.Content.Site().OfType
This snippet of your code here:
on this line:
it needs to pass in a Model Class to the .OfType()
so it would need to be this:
replacing "Home" with your Home Model's class name
I have tried this
@{ var homeNode = Model.Content.Site().OfType
but it gives an error
Object reference not set to an instance of an object. @{ Line 97: var homeNode = Model.Content.Site().OfType
Okay what is the alias of the DocType for your home node?
Sorry for the previous post. My code look like
@{ var homeNode = Model.Content.Site().OfType
Menus named doctype with alias menuBar
Hi Zubair,
Do you have a model for your homepage?
If you do what is the name of the class? (the .cs file)
@{ var homeNode = Model.Content.AncestorOrSelf(1); @Html.Partial("ExampleNavigation", homeNode.MenuBar) }
Still gives compilation error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'MenuBar' and no extension method 'MenuBar' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)
No I haven't any model related to meganav or menus documents (doc type). I have read some other post that are used to make navigation using multinode tree picker but failed..... These navigation class makes thier own models and controllers.
all.generated.cs does not update models definition like I have made Menus.cshtml then I go to all.generated.cs and copy paste the definition of previous model (like category document type) and then change previous model definition to new one (Menus document type). New definition is:
pragma warning disable 0109 // new is redundant
pragma warning restore 0109
pragma warning disable 0109 // new is redundant
pragma warning restore 0109
I just want to ask that is there any problem. Same I have done with models.generated.cs.
is working on a reply...