0
votes

I have the following page structure in Umbraco:

  • Home
    • Device Management
    • Resources
      • Manuals
      • Downloads
      • Terms & Conditions

I have a navigation menu as a partial in my Master.cshtml template which is the base template of all of these pages. In this partial I call Model.Content.Site().Children to get all the "Device Management" and "Resources" nodes then recurse to get any descendant nodes of these.

This all worked fine when all of the pages used the same Document Type (ChildNodeSelectionPage) but now I've changed the Home page to use a derived form of this document type (CustomChildNodeSelectionPage) for an additional property and had to create a dummy controller (CustomChildNodeSelectionPageController) in my project which derives from the original ChildNodeSelectionPageController so that the Master model still gets passed back to the view.

This part also seems to work fine (based upon breakpoints being hit in the original controller) but the problem happens when the view is hit: now, for some unknown reason, Model.Content.Site().Children has a count of 0 when on the Home page but yet, if navigating to the URL of any of the child pages it's 2 (as expected).

Also note that the Home Document still uses the ChildNodeSelectionPage.cshtml Template even though its Document Type has now been changed to use "Custom Child Node Selection Page").

Master.cshtml (note: irrelevant code omitted for brevity)

@inherits Umbraco.Web.Mvc.UmbracoViewPage<Web.Portal.Models.Master>
<!DOCTYPE html>
<html>
  <body>
        @Html.Partial("Navigation Menu")
      @RenderBody()
  </body>
</html>

Navigation Menu.cshtml

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using Umbraco.Web
@using Umbraco.Web.Models
@helper AddMenuItems(IEnumerable<IPublishedContent> menuItems)
{
  if (menuItems.Any())
  {
  <ul>
      @foreach (var menuItem in menuItems)
      {
          <li>
              @if (menuItem.Id == UmbracoContext.PageId)
              {
                  @menuItem.Name
              }
              else
              {
                  <a href="@menuItem.Url" title="@menuItem.GetPropertyValue("description")">@menuItem.Name</a>
              }
              @AddMenuItems(menuItem.Children)
          </li>
      }
  </ul>
  }
}

//NOTE: This is where the problem is when called from the `CustomChildNodeSelectionPage`.
@AddMenuItems(Model.Content.Site().Children)

ChildNodeSelectionPage.cshtml

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = "Shared/Master.cshtml";
}
//NOTE: Irrelevant code omitted for brevity.

ChildNodeSelectionPageController

public class ChildNodeSelectionPageController : RenderMvcController
{
  private ActionResult Index(IPublishedContent content, CultureInfo currentCulture)
      => CurrentTemplate
      (
          new Master
          (
              content,
              currentCulture,
              new Company(0, "ACME"),
              new[]
              {
                  new Company(0, "ACME"),
                  new Company(1, "Jimbo Jones' Jimbo Burgers Inc.")
              },
              "Jiminy Jilikers"
          )
      );

  public override ActionResult Index(RenderModel model)
      => Index
      (
          model.Content,
          model.CurrentCulture
      );
}

CustomChildNodeSelectionPageController

//Exists only so that the new Document Type can call Index on ChildNOdeSelectionPageController.
public class CustomChildNodeSelectionPageController : ChildNodeSelectionPageController
{
}
1

1 Answers

1
votes

So it turns out all I had to do was manually republish ("Save and Publish" button on "Content") every page through the CMS and it magically started working again! Prior to this issue, I did have to change the Content Type of the root page via the database which seemed to create a few problems (of which I'm guessing this was one of them).

If anyone's interested, I got the idea to republish from here: https://our.umbraco.com/forum/templating/templates-and-document-types/4585-Change-document-type