callumbwhyte / meganav

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

Server Errror when referencing the ExampleNavigation.cshtml shipped with meganav #19

Closed blachawk closed 6 years ago

blachawk commented 6 years ago

But when I attempted to link the partial view, ExampleNavigation.cshtml which ships with meganav, onto my Home template like so...

<section>
    @Html.Partial("ExampleNavigation")
</section>

...the rendered web page throws a fit and error logs says....

>  2017-11-28 17:01:05,852 [P19604/D16/T129] ERROR Umbraco.Core.UmbracoApplicationBase - An unhandled exception occurred
> 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]'.
>    at System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value)
>    at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary)
>    at System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData)
>    at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
>    at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
>    at System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData)
>    at ASP._Page_Views_Home_cshtml.Execute() in c:\Users\me\umbraco\umb07\umb07\Views\Home.cshtml:line 21
>    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
>    at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
>    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
>    at Umbraco.Core.Profiling.ProfilingView.Render(ViewContext viewContext, TextWriter writer)
>    at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
>    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
>    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
>    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
>    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
>    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
>    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
>    at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
>    at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
>    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
>    at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
>    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
>    at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
>    at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
>    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
>    at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
>    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
>    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
> 

Do you have any ideas of how I should solve this issue?

callumbwhyte commented 6 years ago

Thank you for raising this issue, I'd be happy to help you out!

The "ExampleNavigation" partial we ship with the package takes a model of "IEnumerable" (which lives within the namespace Cogworks.Meganav.Models) so you'll need to pass in a model for this to work.

Once you've created your Meganav property on a doctype within the CMS, let's say you called it "mainMenu", you simply need to do the following to load in the Meganav to your page:

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

Or if you're using ModelsBuilder, it should be possible to simply pass Model.Content.MainMenu like so:

@Html.Partial("ExampleNavigation", Model.Content.MainMenu)

I assume you checked out the "Integration guide" that's included within this repository to find the "ExampleNavigation" partial. On reflection this document isn't clear enough about how to implement the property – I will update the docs now!

Thanks again!

blachawk commented 6 years ago

Many many thanks! - Your response was just what I needed. Issue solved. And thanks for the soon to come update on the document too.

callumbwhyte commented 6 years ago

You're very welcome! #h5yr