Null reference thrown when mapping fields on interfaces

23 views
Skip to first unread message

Oliver Vaughan

unread,
Aug 18, 2014, 10:16:59 AM8/18/14
to glass...@googlegroups.com
I'm mapping part of my content tree using Glass and Sitecore 7.2 in MVC.

I have used your example for building navigation by specifying a model as shown:

[SitecoreType(AutoMap = true, TemplateId = "{9841BF49-DB00-4678-A43C-1BE37BA3D9C7}")]
    public interface IBaseNavigation : IBase
    {
        Boolean ExcludeFromLeftNav { get; set; }
    }



[SitecoreType(AutoMap = true)]
    public interface IMenuItemModel : IBaseNavigation
    {
        string Title { get; set; }

        Link Link { get; set; }

        IEnumerable<IMenuItemModel> Children { get; set; }
    }

When I build the navigation elements using this structure all works well unless there is (obviously) an element that doesn't contain the ExcludeFromLeftNav field.

My question is how to check for this field when it is being retrieved via a proxy? The mapper doesn't throw an error when the call is made to sitecore it only fails here as a null reference:

        foreach (var item in Model.Children)
        {
            if(item.ExcludeFromLeftNav || String.IsNullOrEmpty(item.Title))
            {

and if I try to do this:

        foreach (var item in Model.Children.Where(x => x.ExcludeFromLeftNav != null))
        {

the compiler tells me that ExcludeFromLeftNav is never null (fair enough as the field is in the model). I would attach a template but I'm only matching several fields of more complex templates and I would like to be as generic as possible in my specification of fields.

Any advice? 

Thanks

Ollie




Reply all
Reply to author
Forward
0 new messages