Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Liam Dilley 172 posts 402 karma points
    Apr 12, 2018 @ 06:26
    Liam Dilley
    0

    Cogworks Meganav - Child templates

    Hi All, New to Umbraco, Just about 10 hours in and learning, Bare with me if anything is "noobish".

    I have a master page, child template setup and running into issues with some inheritance etc.

    Master Template has:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Umbraco.Core
    @{
        Layout = null;
    }
    

    I am calling a partial:

    @Html.CachedPartial("Header", Model.Content, 3600, true)
    

    This renders. In this partial I have:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @using Umbraco.Core
    

    I have a layout and for a navigation I call a Navigation Partial:

    @Html.Partial("~/Views/Partials/Navigation/TopNavigation.cshtml")
    

    I am using this megaNav: https://github.com/thecogworks/meganav

    And its code they provide the basic loop through and here is where I have my current issue.

    At the top of this partial I have:

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

    And the for loop:

    @foreach (var item in Model.Content.GetPropertyValue<IEnumerable<MeganavItem>>("navigation"))
    

    When I am on my homepage document type

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    @{
        Layout = "Master.cshtml";
    }
    

    The navigation renders fine.

    But when I am on a page using the page document type the site produces an error.

    enter image description here

    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 8:  <div class="nav navv2">
    Line 9:      <ul>
    Line 10:     @foreach (var item in Model.Content.GetPropertyValue<IEnumerable<MeganavItem>>("navigation"))
    Line 11:     {
    Line 12:         if (item.Children.Any()){
    

    This line:

    @foreach (var item in Model.Content.GetPropertyValue<>

    Can anyone help me with the issue please?

  • kows 81 posts 151 karma points c-trib
    May 08, 2018 @ 07:08
    kows
    0

    Model.Content is equal to your current page. You are accessing the "navigation" property each time on the page itself. I'm guessing you have this property only available on your "Home" node. You should determine what the "Home" node is for your current page. Most probably, by traversing:

    Model.Content.AncestorOrSelf("homeDocType")
    

    You should directly pass it in the call (for the caching).

Please Sign in or register to post replies

Write your reply to:

Draft