0

Im having a problem with Glass Mapper, mapping/casting an item to a custom model.

The Model with the complextype property (snippet):

[SitecoreType(TemplateId = Templates.IDString, AutoMap = true)]
public class EmployeeModel
{
    ///other propertes hidden 

    [SitecoreChildren(IsLazy = true)]
    [SitecoreField(FieldId = Templates.Items.Office.Fields.BusinessOfficeString)]
    public virtual ItemViewModel Office { get; set; }
}

The ItemViewModel:

 [SitecoreType(AutoMap =true)]
 public class ItemViewModel
 {
    public virtual ID Id { get; set; }

    public virtual string Template { get; set; }

    public virtual string Name { get; set; }

    public virtual string Url { get; set; }

    public virtual Dictionary<string, IFeatureModel> Features { get; set; }
}

To create this EmployeeModel im casting an Sitecore Item to this model:

var featureModel = _sitecoreService.Cast<EmployeeModel>(item);

But this call is throwing an error:

The type Foundation.Macaw.Modeling.Models.ViewModels.ItemViewModel does not contain any generic arguments

I think this is caused by the [SitecoreChildren(IsLazy = true)] because when i remove this line it works. However without that line an stackoverflow can be caused since the OfficeModel has a reference to Employee...

When casting directly to an ItemViewModel the same item works like a charm:

    var featureModel = _sitecoreService.Cast<EmployeeModel>(item);

Any one knows what could solve my problem?

FYI: im using Sitecore 9 Update 1 and glassmapper 4.5.04

Tnx!

2
  • In the meantime i had a brainfart and learned some stuff. Office is ofcourse not a child but a linked item so i changed this to '[SitecoreLinked(IsLazy = true)]'. but then i got the error _Could not find a data mapper to handle property AbstractPropertyConfiguration Property: Office Type: Feature.Macaw.Person.Models.EmployeeModel also im using a custom mapper to map the ItemViewModel but this is causing the reference loop since the Glass mapping starts over and over so the depthcheck doest get triggered. Im figuring ou now to work around this... _
    – Tom Neij
    Commented Feb 16, 2018 at 15:29
  • I think you need to edit this question and clarify a bit. You mention OfficeModel, but don't provide code for it. Where does that come into play? What exactly is your question at this point?
    – Ben Golden
    Commented Feb 16, 2018 at 17:30

1 Answer 1

2

When you use [SitecoreChildren] Glass expects the property to be an IEnumerable<T> or IList<T>. You also should not use two different Glass attributes on the same property. You can map a property from a field or from child items, but not both.

Update: I just added this other question and answer that shows the different attributes and their uses, which may be helpful for you.

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.