0

The below code is not returning the children's,

var TemplateID = Guid.Parse("{51A26CD4-FCB5-4A3C-8720-2D60F625A678}");           
var _Options = new GetItemByIdOptions(TemplateID)
{
    InferType = true,
    Lazy = Glass.Mapper.LazyLoading.Enabled
};
var target = context.SitecoreService.GetItem<IGlassModel>(_Options);

Below is the Model :

[SitecoreType(TemplateId = "{19CDADF8-D474-42C9-8C8D-522A7FF93663}", AutoMap=true)]
    public class IGlassModel
    {
        Guid Id { get; set; }

        string Path { get; set; }

        //[SitecoreItem]
        //string Name { get; set; }

        [SitecoreChildren]
        IEnumerable<Country> Chlidren { get; set; }

    }       

    [SitecoreType(TemplateId = "{780F8593-3AFA-4DF0-98EE-16FFDD18F88F}", AutoMap = true)]
    public class Country
    {
        Guid Id { get; set; }

        string Path { get; set; }

        [SitecoreItem]
        string CountryName { get; set; }

        [SitecoreItem]
        string CountryCode { get; set; }
    }
3
  • can you please share also the IGlassModel? Commented Jun 1, 2021 at 16:42
  • @TamásTárnok, Updated with IGlassModel. Commented Jun 1, 2021 at 17:45
  • 1
    Try adding virtual modifier for Children
    – Marek Musielak
    Commented Jun 1, 2021 at 19:48

1 Answer 1

1

We faced the same issue any adding virtual keyword worked for us as @Marek is saying in comments as well.

[SitecoreChildren]
public virtual IEnumerable<Country> Chlidren { get; set; } 

Glass mapper document states - One important thing to remember when writing class with Glass.Mapper is to ensure that all your fields are marked as virtual. Glass.Mapper can automatically infer what data to load for your model by using a combination of On Demand mapping and Auto-Mapping.

You can refer blog that is explaining why all Glass mapper field should be virtual - https://stackoverflow.com/questions/33105212/why-should-all-glass-mapper-fields-be-virtual

1
  • Added virtual keyword. but now i'm getting this error "error CS0234: The type or namespace name 'IGlassModelProxy' does not exist in the namespace 'Castle.Proxies' (are you missing an assembly reference?)" Commented Jun 2, 2021 at 14:41

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.