Try our conversational search powered by Generative AI!

Serious issue with Visitor-Group criterion if model type is nullable enum

Vote:
 

I noticed an issue that prevents the website from starting if you have a criterion model with an enum type property and use the EnumSelectionFactory(or a custom one):

Invalid cast from 'System.Int32' to 'CustomerName.Corporate.Salesforce.FinancialServiceCloud.Enums.VisitorGroup'.

This is the model class(reduced to the essential):

[EPiServerDataStore(AutomaticallyRemapStore = true)]
public class UserProfileCriterionSettings : CriterionModelBase
{
	[CriterionPropertyEditor(
		LabelTranslationKey = "visitorgroups/criteria/userprofile/usertypes",
		SelectionFactoryType = typeof(EnumSelectionFactory), Order = 30)]
	public VisitorGroup? UserType { get; set; }

        [CriterionPropertyEditor(
		LabelTranslationKey = "visitorgroups/criteria/userprofile/domiciles",
		SelectionFactoryType = typeof(DomicileSelectionFactory), Order = 20)]
	public virtual string? Domicile { get; set; }

	public override ICriterionModel Copy()
	{
		return ShallowCopy();
	}
}

So both properties are optional which means i need to support Nullable<VisitorGroup>. That is a custom enum, so don't confuse it with the class EPiServer.Personalization.VisitorGroups.VisitorGroup.

I have found a workaround. I will split this criterion into two where both properties are required in each. But i wonder how to support nullable enums in visitor-group-criteria.

#313722
Edited, Dec 05, 2023 16:46
Vote:
 

Int by itself is a primitive and not a reference type and as such will always have a value.  In this case why not simply have an enum with a "None" value which maps to 0 instead? You can then validate against the value of None when making your decisions.

e.g.

public enum VisitorGroup
{
   None = 0,
   ValueOne,
   ValueTwo
}
#314462
Dec 20, 2023 16:30
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.