1

I'm trying to get items from Sitecore using GlassMapper 5 and after trying in many ways the properties in my object are always null.

Here are my templates.

enter image description here

And this is my content tree structure

enter image description here

The MainNav item is being used as datasource of a rendering and this how I'm getting it in the controller.

    public ActionResult NavigationHeader() 
    {
        var ds = MvcContext.GetDataSourceItem<MainNav>();
        if (ds == null) 
        {
            return null;
        }

        return View("~/Views/Redesign/Navigation/NavigationHeader.cshtml", ds);
    }

I've tried to disable lazy loading

var ds = MvcContext.GetDataSourceItem<MainNav>(new GetKnownOptions { Lazy = Glass.Mapper.LazyLoading.Disabled })

But by doing that an exception is thrown Model depth check failed. Model graph too large, enable lazy loading.

Here is how my MainNav class is defined

[SitecoreType(TemplateId = "{93110014-14DC-4380-B031-54A31358AD9E}")]
public class MainNav : GlassMapperBase
{
    [SitecoreField(FieldId = "{63A954E5-5A8F-46DE-B361-7C2CEB908F8A}")]
    public Image Logo { get; set; }

    [SitecoreChildren]
    public IEnumerable<INavigation> Navigation { get; set; }
}

I've already tried to enable AutoMap and EnforceTemplate.

I've already checked the Template Id as well as the Field Id and they are correct.

I've already removed the SitecoreType annotation from MainNav class.

I've already checked the MainNav Item versions and there is only one.

I've already checked the web database and everything is exactly like in the master.

I've already checked the language and both, the site definition and the Sitecore.Context.Language are defined as "en".

In the view, when I try to access Model.Logo or Model.Navigation, those properties are always null but in the immediate window, I can access TemplateId or Name properties defined in GlassMapperBase class.

I really don't know what else to try.

1 Answer 1

1

All your properties must be virtual, e.g.

[SitecoreField(FieldId = "{63A954E5-5A8F-46DE-B361-7C2CEB908F8A}")]
public virtual Image Logo { get; set; }

Why? Glass mapper generates a proxy class of your class dynamically. In order to make sure that proxy class can override the property, it has to be marked as virtual.

See Glass Mapper documentation examples:

https://training.glass.lu/courses/367234/lectures/5610541

Also notice that all the properties on a class use the virtual keyword. This is required. If you don't mark your properties as virtual, Glass.Mapper can't lazy load data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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