1

I am building a navigation component using glass mapper. The problem I am facing is not being able to get children items of a navigation item.

Right now I am only getting Parent navigation items.

This is my model:

[SitecoreType(TemplateId = "{127AF5CF-4170-4E43-A8CC-DB89591954E2}", AutoMap = true)]

public class NavigationItem
{
    public NavigationItem()
    {
        ChildNavigations = new List<NavigationItem>();
    }

    [SitecoreField]
    public virtual string Title { get; set; }

    [SitecoreField(FieldType = SitecoreFieldType.GeneralLink)]
    public virtual Link Url { get; set; }

    [SitecoreField(FieldName = "Is Excluded")]
    public virtual bool IsExcluded { get; set; }

    [SitecoreChildren(InferType = true)]
    public List<NavigationItem> ChildNavigations { get; set; }
}

    public ActionResult RenderNavigation()
    {
        var mvcContext = new MvcContext();

        var currentItem = Sitecore.Context.Database.GetItem(ID.Parse("{64121195-F56F-4A8B-B8F9-5A1E69BA62FD}"));

        IEnumerable<NavigationItem> navigationItems = currentItem.Children
            .Where(O => O.TemplateID.Equals(ID.Parse("{127AF5CF-4170-4E43-A8CC-DB89591954E2}")))
            .Select(x => mvcContext.SitecoreService.GetItem<NavigationItem>(x));

        return View(navigationItems);
    }

enter image description here

4

I assume that you are using Glass Mapper v4.

In order to fix your problem change your definition of ChildNavigations to this:

[SitecoreChildren(IsLazy = false)]
public virtual IEnumerable<NavigationItem> ChildNavigations { get; set; }

There was a change for v5 and you need to do it differently: http://glass.lu/Mapper/Documentation/UpgradingToV5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.