callumbwhyte / meganav

A flexible, draggable link picker for constructing site navigation menus in Umbraco
MIT License
35 stars 34 forks source link

How to render partial "ExampleNavigation" in Master template? #23

Closed mjharaldsson closed 6 years ago

mjharaldsson commented 6 years ago

Hi,

Sorry for a stupid question, but how do I render the partial "ExampleNavigation" in the master template?

On my Master template I try to render the partial like so:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{ 
    Layout = null;
}

@Html.Partial("ExampleNavigation", Model.Content.GetPropertyValue<IEnumerable<MeganavItem>>("topNavigation"))

But the MeganavItem shows the error "The type or namespace 'MeganavItem' could not be found". So I tried adding the namespace like so:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Cogworks.Meganav.Models

@{ 
    Layout = null;
}

@Html.Partial("ExampleNavigation", Model.Content.GetPropertyValue<IEnumerable<MeganavItem>>("topNavigation"))

This gives me no error in Visual Studio, but instead this Server Error message:

The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Cogworks.Meganav.Models.MeganavItem]'.

Can you help me solve this please? :)

callumbwhyte commented 6 years ago

Hello,

Thank you for using Meganav, I'm sorry you're having some issues getting up and running!

The steps you've gone through so far and your final code sample look good to me, it's very difficult to see where this problem might be coming from as the code looks correct.

My guess would be that Model.Content.GetPropertyValue<IEnumerable<MeganavItem>>("topNavigation") is returning a null which is then passed through to the partial. Does a property definitely exist on the node you are testing on with the alias "topNavigation"?

mjharaldsson commented 6 years ago

Hi again,

Thank you for your quick reply!

Yes, propertiy exists on the node I'm testing. I noticed that the navigation renders on the "Home Page" but when I navigate to one of the child pages I get the following error message:

The model item passed into the dictionary is of type 'Umbraco.Web.Models.RenderModel', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Cogworks.Meganav.Models.MeganavItem]'.

This is how it renders on the Home Page:

Very weird and I'm 100% sure I'm doing something really stupid, just have to figure out what :).

callumbwhyte commented 6 years ago

Ah I think I see what's going on now!

In your master template you call Model.Content.GetPropertyValue<IEnumerable<MeganavItem>>("topNavigation") to get your navigation. Here Model.Content is the IPublishedContent of the page you are currently on. As the Meganav exists on your homepage you can only retrieve the property data from the IPublishedContent of the homepage - if you navigate to a different page the model that is returned will be the IPublishedContent for that other page where the Meganav does not exist.

Ultimately you need to fetch the Meganav from elsewhere in the tree rather than just the current page as it's not always going to be there.

My recommended approach would be to get the IPublishedContent for the homepage node by XPath in your Master template, like this...

@{
    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var homepage = umbracoHelper.TypedContentSingleAtXPath("//Homepage")
}

...And then just use the new homepage variable instead of Model.Content, like this:

@Html.Partial("ExampleNavigation", homepage.GetPropertyValue<IEnumerable<MeganavItem>>("topNavigation"))

I hope this helps point you in the right direction!

mjharaldsson commented 6 years ago

That did the trick! đź‘Ť

Thank you so very much for your support and excellent package! It really makes it so much easier to build more complex and flexible menu structure for your website.

The editors are gonna be so happy now! :)

callumbwhyte commented 6 years ago

Glad I could help, enjoy using Meganav!