Copied to clipboard

Flag this post as spam?

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


  • Jimmy Dan Mortensen 77 posts 197 karma points
    Feb 18, 2018 @ 08:28
    Jimmy Dan Mortensen
    0

    Navigation Snippet in Umbraco 7.8.1 returns Object reference not set to an instance of an object.

    Hi gurus :-)

    I am by no means an experienced coder, so I try and "steal" what I can :-). I am new to Umbraco 7, so I am fiddling with an empty Umbraco.

    I have installed Archetype from Packages as the only thing.

    I have created a base template, a Home doc-type & template and a page doc-type & template. I also have an Controller that returns my partials. I have a Navigation partial, that I am rendering from my header-partial on the Base-template via @{Html.RenderAction("RenderNavigation", "SiteLayout").

    In Content I have my Home and a single page published. When I try and use the Navigation snippet from Partial Views i get this error on every page:

    Object reference not set to an instance of an object.

    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 2: @using Umbraco.Web

    Line 3:

    Line 4: @{ var selection = Model.Content.Site().Children().Where(x => x.IsVisible()); }

    Line 5:

    Line 6: @if (selection.Count() > 0)

    Source File: c:\inetpub\wwwroot\lie-mortensen.dk\Lie-Mortensen\Views\Partials\SiteLayout_Navigation.cshtml Line: 4

    Any hints to what is wrong and how I can move on would be greatly appreciated :-)

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Feb 18, 2018 @ 12:44
    Nik
    0

    Hi Jimmy,

    Could you share all the code from within SiteLayout_Navigation.cshtml? This would give us a better overall view of what code is running :-)

    On a side note, I would advise that you carefully consider the use of Archetype. Although it is a great package, it has now been sunset in favor of Nested Content which is part of the Umbraco Core now. As such, it is unlikely for any further bugfixes/developments/compatibility issues to be fixed with Archetype.

    Nik

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Feb 18, 2018 @ 13:40
    Jimmy Dan Mortensen
    0

    Hi Nik

    Here is my _navigation.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Umbraco.Web
    @{ var selection = Model.Content.Site().Children().Where(x => x.IsVisible()); }
    
    @if (selection.Count() > 0)
    {
        <nav class="consult-nav">
            <ul class="consult-menu">
                @foreach (var item in selection)
                {
                    <li class="@(item.IsAncestorOrSelf(Model.Content) ? "current-menu-item" : null)">
                        <a href="@item.Url">@item.Name</a>
                    </li>
                }
            </ul>
        </nav>
    }
    

    Interesting about Archetype :-) Any link to where I can read more about Nested Content?

    Best regards Jimmy

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Feb 18, 2018 @ 17:20
    Nik
    0

    Hi Jimmy,

    Okay, so based on the error and looking at your code I think one of the following is occuring:

    1. Either Model.Content is null
    2. Model.Content.Site() is null (although this is unlikely)
    3. Model.Content.Site().Children() is null.

    I would start by checking which of these are null but setting them to individual variables so:

    @{
        var contentIsNull = Model.Content == null;
    }
    

    Then using a break point checking if this is true, them moving onto the next one.

    With regards to nested content:

    This is the original package: https://our.umbraco.org/projects/backoffice-extensions/nested-content/ This is additional documentation since it was brought into the Core: https://our.umbraco.org/documentation/getting-started/backoffice/Property-Editors/Built-in-Property-Editors/Nested-Content

    Nik

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Feb 19, 2018 @ 08:14
    Jimmy Dan Mortensen
    0

    Hi Nik

    My Model.Content apparrently returns Nullexception.

    var contentIsNull = Model.Content == null; returns this message:

    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    
    System.Web.Mvc.WebViewPage<TModel>.Model.get returned null.
    

    Does this makes any sense?

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Feb 19, 2018 @ 08:30
    Nik
    0

    Hi Jimmy,

    Nope, that is very odd.

    Can you share what your layout file looks like which calls this navigation partial?

    Thanks,

    Nik

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Feb 19, 2018 @ 09:16
    Jimmy Dan Mortensen
    0

    Hi Nik

    This is my Base.cshtml

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Home</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta name="format-detection" content="telephone=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <!-- Fonts-->
        <link rel="stylesheet" type="text/css" href="/fonts/fontawesome/font-awesome.min.css">
        <link rel="stylesheet" type="text/css" href="/fonts/themify-icons/themify-icons.css">
        <!-- Vendors-->
        <link rel="stylesheet" type="text/css" href="/Vendors/bootstrap4/bootstrap-grid.min.css">
        <link rel="stylesheet" type="text/css" href="/Vendors/magnific-popup/magnific-popup.min.css">
        <link rel="stylesheet" type="text/css" href="/Vendors/owl.carousel/owl.carousel.css">
        <link rel="stylesheet" type="text/css" href="/Vendors/_jquery/jquery.min.css">
        <!-- App & fonts-->
        <link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i&amp;amp;subset=latin-ext">
        <link rel="stylesheet" type="text/css" href="/css/main.css">
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <![endif]-->
    </head>
    
    <body>
        <div class="page-wrap">
    
            <!-- header -->               
            @{Html.RenderAction("RenderHeader", "SiteLayout");}
            <!-- End / header -->
            <!-- Content-->
            @RenderBody();
            <!-- End / Content-->
            <!-- footer -->
            <footer class="footer">
                <div class="footer__main">
                    <div class="row row-eq-height">
                        <div class="col-8 col-sm-7 col-md-9 col-lg-3 ">
                            <div class="footer__item">
                                <a class="consult_logo" href="#"><img src="/img/logo.png" alt="" /></a>
                                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut laoreet ut lacus a tincidunt.</p>
                            </div>
                        </div>
                        <div class="col-sm-6 col-md-4 col-lg-3 col-xl-2 offset-0 offset-sm-0 offset-md-0 offset-lg-0 offset-xl-1 ">
                            <div class="footer__item">
    
                                <!-- widget-text__widget -->
                                <section class="widget-text__widget widget">
                                    <div class="widget-text__content">
                                        <ul>
                                            <li><a href="#">Term of Services </a></li>
                                            <li><a href="#">Privacy Policy </a></li>
                                            <li><a href="#">Sitemap </a></li>
                                            <li><a href="#">Help</a></li>
                                        </ul>
                                    </div>
                                </section><!-- End / widget-text__widget -->
    
                            </div>
                        </div>
                        <div class="col-sm-6 col-md-4 col-lg-2 col-xl-2 ">
                            <div class="footer__item">
    
                                <!-- widget-text__widget -->
                                <section class="widget-text__widget widget">
                                    <div class="widget-text__content">
                                        <ul>
                                            <li><a href="#">How It Work </a></li>
                                            <li><a href="#">Carrier </a></li>
                                            <li><a href="#">Pricing </a></li>
                                            <li><a href="#">Support</a></li>
                                        </ul>
                                    </div>
                                </section><!-- End / widget-text__widget -->
    
                            </div>
                        </div>
                        <div class="col-md-4 col-lg-2 col-xl-2 ">
                            <div class="footer__item">
    
                                <!-- form-sub -->
                                <div class="form-sub">
                                    <h4 class="form-sub__title">Our Newsletter</h4>
                                    <form class="form-sub__form">
                                        <div class="form-item">
                                            <input class="form-control" type="email" placeholder="Enter Your Email Address..." />
                                        </div>
                                        <div class="form-submit">
                                            <button class="form-button" type="submit"><i class="fa fa-paper-plane" aria-hidden="true"></i></button>
                                        </div>
                                    </form>
                                </div><!-- End / form-sub -->
    
                            </div>
                        </div>
                        <div class="col-sm-6 col-md-4 col-lg-2 col-xl-2  consult_backToTop">
                            <div class="footer__item"><a href="#" id="back-to-top"> <i class="fa fa-angle-up" aria-hidden="true"> </i><span>Back To Top</span></a></div>
                        </div>
                    </div>
                </div>
                <div class="footer__copyright">2017 &copy; Copyright Awe7. All rights Reserved.</div>
            </footer><!-- End / footer -->
    
        </div>
        <!-- Vendors-->
        <script type="text/javascript" src="/Vendors/jquery/jquery.min.js"></script>
        <script type="text/javascript" src="/Vendors/imagesloaded/imagesloaded.pkgd.js"></script>
        <script type="text/javascript" src="/Vendors/isotope-layout/isotope.pkgd.js"></script>
        <script type="text/javascript" src="/Vendors/jquery.countdown/jquery.countdown.min.js"></script>
        <script type="text/javascript" src="/Vendors/jquery.countTo/jquery.countTo.min.js"></script>
        <script type="text/javascript" src="/Vendors/jquery.countUp/jquery.countup.min.js"></script>
        <script type="text/javascript" src="/Vendors/jquery.matchHeight/jquery.matchHeight.min.js"></script>
        <script type="text/javascript" src="/Vendors/jquery.mb.ytplayer/jquery.mb.YTPlayer.min.js"></script>
        <script type="text/javascript" src="/Vendors/magnific-popup/jquery.magnific-popup.min.js"></script>
        <script type="text/javascript" src="/Vendors/masonry-layout/masonry.pkgd.js"></script>
        <script type="text/javascript" src="/Vendors/owl.carousel/owl.carousel.js"></script>
        <script type="text/javascript" src="/Vendors/jquery.waypoints/jquery.waypoints.min.js"></script>
        <script type="text/javascript" src="/Vendors/menu/menu.min.js"></script>
        <script type="text/javascript" src="/Vendors/smoothscroll/SmoothScroll.min.js"></script>
        <!-- App-->
        <script type="text/javascript" src="/Scripts/main.js"></script>
    </body>
    </html>
    

    The @HTML.RenderAction calls from this file (SiteLayoutController.cs)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    namespace Lie_Mortensen.Controllers
    {
        public class SiteLayoutController : SurfaceController
        {
            private const string PARTIAL_VIEW_FOLDER = "~/Views/Partials/SiteLayout/";
    
            public ActionResult RenderHeader()
            {
                return PartialView(PARTIAL_VIEW_FOLDER + "_Header.cshtml");
            }
    
            public ActionResult RenderNavigation()
            {
                return PartialView(PARTIAL_VIEW_FOLDER + "_Navigation.cshtml");
            }
        }
    }
    

    Which then returns _Header.cshtml that looks like this right now:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    
    <header class="header header__style-02">
        <div class="container">
            <div class="header__logo"><a href="/"><img src="/img/logo.png" alt="" /></a></div>
            <div class="header__toogleGroup">
                @*<div class="header__chooseLanguage">
    
                    <!-- dropdown -->
                    <div class="dropdown" data-init="dropdown">
                        <a class="dropdown__toggle" href="javascript:void(0)">EN <i class="fa fa-angle-down" aria-hidden="true"></i></a>
                        <div class="dropdown__content" data-position="right">
                            <ul class="list-style-none">
                                <li><a href="#">EN</a></li>
                                <li><a href="#">DE</a></li>
                                <li><a href="#">VI</a></li>
                            </ul>
                        </div>
                    </div><!-- End / dropdown -->
    
                </div>*@
                <div class="search-form">
                    <div class="search-form__toggle"><i class="ti-search"></i></div>
                    <div class="search-form__form">
    
                        <!-- form-search -->
                        <div class="form-search">
                            <form>
                                <input class="form-control" type="text" placeholder="Hit enter to search or ESC to close" />
                            </form>
                        </div><!-- End / form-search -->
    
                    </div>
                </div>
            </div>
    
            <!-- consult-nav -->
            @{
                var contentIsNull = Model.Content == null;
                var SiteIsNull = Model.Content.Site() == null;
                var ChildrenIsNull = Model.Content.Site().Children() == null;
                var ChildrenWIsNull = Model.Content.Site().Children().Where(x => x.IsVisible()) == null;
            }
    
    
            @*@{Html.RenderAction("RenderNavigation", "SiteLayout");}*@
            <!-- End / consult-nav -->
    
        </div>
    </header>
    

    My Navigation file commented out, so I think you have all the code right now. I bet it is something really stupid, but I really can't figure out what is wrong.

    Best regards Jimmy

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Feb 19, 2018 @ 09:22
    Nik
    100

    Hi Jimmy

    Ahh, You aren't passing a model to your partial view which is the problem.

    As your navigation action doesn't appear to perform any logic you could simply do this in your base.cshtml:

    @Html.Partial("SiteLayout/_Navigation.cshtml")
    

    (Can't remember is if it Html.Partial or Html.PartialView sorry)

    This should then fix the issue.

    However if you are looking to perform some logic in your controller action, you will need to pass a model to your view.

    Nik

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Feb 19, 2018 @ 11:08
    Jimmy Dan Mortensen
    0

    Hi Nik

    Thanks for pointing out the error :-) Here comes the total newbie question then :-)

    Can I use the modelsbuilder in Umbraco in any way, or do I need to create a model myself?

  • Nik 1617 posts 7263 karma points MVP 7x c-trib
    Feb 19, 2018 @ 11:33
    Nik
    0

    Hi Jimmy,

    In what way do you want to use the ModelsBuilder? The Models builder creates strongly typed models for your document types, but if you want a custom model, say "MyNavigationModel" then you would just create your own model as per general MVC.

    You could then tell your navigation view to inherit from:

    UmbracoViewPage<MyNavigationModel>
    

    This would allow you to pass in your own custom model to the partial view from your action.

    Nik

  • Jimmy Dan Mortensen 77 posts 197 karma points
    Feb 19, 2018 @ 13:28
    Jimmy Dan Mortensen
    0

    Hi Nik,

    Just in the way, that I don't know how to make my own model as of yet :-) But I'll google it and search for MVC models :-)

Please Sign in or register to post replies

Write your reply to:

Draft