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()); }
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.
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:
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.
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.
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:
Any hints to what is wrong and how I can move on would be greatly appreciated :-)
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
Hi Nik
Here is my _navigation.cshtml
Interesting about Archetype :-) Any link to where I can read more about Nested Content?
Best regards Jimmy
Hi Jimmy,
Okay, so based on the error and looking at your code I think one of the following is occuring:
I would start by checking which of these are null but setting them to individual variables so:
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
Hi Nik
My Model.Content apparrently returns Nullexception.
var contentIsNull = Model.Content == null;
returns this message:Does this makes any sense?
Hi Jimmy,
Nope, that is very odd.
Can you share what your layout file looks like which calls this navigation partial?
Thanks,
Nik
Hi Nik
This is my Base.cshtml
The @HTML.RenderAction calls from this file (SiteLayoutController.cs)
Which then returns _Header.cshtml that looks like this right now:
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
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:
(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
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?
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:
This would allow you to pass in your own custom model to the partial view from your action.
Nik
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 :-)
is working on a reply...