4

I am trying to use Glass.Mapper's InferType property. I've read through the tutorial on the Glass website. I have two alert types that have different markup. They both inherit from the same base class AssetAlert that contains a property that holds the alert message. This code does not work as it is and when I debug it, the type returned is of the base class instead of the appropriate derived class. I have already updated GlassMapperScCustom.cs like the tutorial says.

How can I confirm that Glass has loaded all of the models from my assembly properly?

And if that's not the issue, can anyone see anything wrong with my code that might be preventing it from inferring the correct derived class instead of the base class?

I'm using Glass.Mapper 4.3.4.196 and Sitecore 8.2 rev. 161115

I have these classes:

FundDetail.cs

// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
[SitecoreType(AutoMap = true, TemplateId = "{1813C6E1-72AD-41B9-9D68-372A843BCCCB}")]
public class FundDetail : StandardItem
{
    [SitecoreQuery("./Alerts/*", IsRelative = true, InferType = true)]
    public virtual IEnumerable<AssetAlert> AssetAlerts { get; [UsedImplicitly] set; }
}

AssetAlert.cs

// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
[SitecoreType(AutoMap = true, TemplateId = "{607D5F70-D3A5-4690-A2EC-259A4700D2E3}")]
public class AssetAlert : StandardItem
{
    [SitecoreField("{0797970F-D9E6-4FB2-86EA-CF271CCA0629}")]
    public virtual string Message { get; [UsedImplicitly] set; }
}

AssetUpdateAlert.cs

[SitecoreType(TemplateId = "{9DAE0D23-6524-4D31-B23E-B4D16E1ADDE0}")]
public class AssetUpdateAlert : AssetAlert
{
    private const string DictionaryFolder = "/Content Types/Asset Alert/";
    public static string Title
        => DictionaryPhraseRepository.Current.Get(DictionaryFolder + "Update Title");
}

AssetShareholderInformationAlert.cs

[SitecoreType(TemplateId = "{2B5FB48B-4ED8-4B3A-8D29-AFDF5A3D66FB}")]
public class AssetShareholderInformationAlert : AssetAlert
{
    private const string DictionaryFolder = "/Content Types/Asset Alert/";
    public static string Title
        => DictionaryPhraseRepository.Current.Get(DictionaryFolder + "Shareholder Information Title");
}

FundDetail.cshtml (I removed the markup for readability)

@if (Model.AssetAlerts != null)
{
    foreach (var alert in Model.AssetAlerts)
    {
        @if (alert is AssetUpdateAlert)
        {
            @AssetUpdateAlert.Title
            @Editable(alert, a => a.Message)
        }
        else if (alert is AssetShareholderInformationAlert)
        {
            @AssetShareholderInformationAlert.Title
            @Editable(alert, a => a.Message)
        }
    }
}
12
  • does putting the AutoMap = true to derived types resolve your issue? Feb 8, 2017 at 15:00
  • I added AutoMap = true per your suggestion, but am getting the same results as before. When I debug the application, I can see that the item is still of type AssetAlert.cs.
    – Iceape
    Feb 8, 2017 at 15:12
  • Does this kind of cast work for you with automap? alert as AssetUpdateAlert Feb 8, 2017 at 15:19
  • Hmm, no it does not. I added "var testalert = alert as AssetUpdateAlert;" to my view inside of the foreach loop and in the debugger testalert is null. I assume that means it was not able to cast alert to AssetUpdateAlert?
    – Iceape
    Feb 8, 2017 at 15:39
  • check if there are actually items of needed templates and also that they are published Feb 8, 2017 at 15:58

1 Answer 1

3

I had the same problem before. After adding the below lines to GlassMapperScCustom class, the InferType attribute works.

public static IConfigurationLoader[] GlassLoaders()
{
    var attributes = new AttributeConfigurationLoader("your assembly name where the models locate");

    return new IConfigurationLoader[] { attributes };
}

Your Answer

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

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