Visitor Group Criteria using enum property on model

Vote:
 

Hi.

I'm creating a criterion where I want to use a enum value as input. In order to create a dropdown to choose from I call my selection factory. But when I decorate my model property to use a selection factory, I get an error.

Following is my code.

Criterion:

public class IsUserCategoryCriterionSettings : CriterionModelBase
    {
        [Required, DojoWidget(SelectionFactoryType = typeof(EnumCategorySelectionFactory))]
        public MyEnum Category { get; set; }

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

I get follwong error:

#249769
Mar 09, 2021 10:22
Vote:
 

Try

public class IsUserCategoryCriterionSettings : CriterionModelBase
    {
        [DojoWidget(SelectionFactoryType = typeof(EnumCategorySelectionFactory))]
        [Required]        
        public MyEnum Category { get; set; }
       //or
        [DojoWidget(SelectionFactoryType = typeof(EnumSelectionFactory))]
        [Required]        
        public MyEnum Category { get; set; }

        public override ICriterionModel Copy()
        {
            return ShallowCopy();
        }
    }
#249780
Mar 09, 2021 18:50
Vote:
 

Hi,

I wonder whether this might be down to the type of SelectionFactory you're using. For visitor group criteria your selection factory needs to be an implementation of EPiServer.Personalization.VisitorGroups.ISelectionFactory whereas for properties on content types, you need an instance of EPiServer.Shell.ObjectEditing.ISelectionFactory. It's also worth noting that, for visitor group criteria there's already an inbuilt Enum selection factory called EPiServer.Web.Mvc.VisitorGroups.EnumSelectionFactory which can be used like this:

[DojoWidget(
    SelectionFactoryType = typeof(EnumSelectionFactory),
    AdditionalOptions = "{ selectOnClick: true }"),
    Required]
public MyEnum Category { get; set; }
#249812
Mar 10, 2021 9:08
Asim - Mar 10, 2021 12:03
That was the issue. Thank you Paul, and the rest of you for giving valuable input :)
Vote:
 

Try this-

public enum OrganizationTypeEnums
    {
        [Description("Internal")]
        Internal,
        [Description("External")]
        External,

    }
 public class OrgTypeSelectionFactory : ISelectionFactory
    {
        public IEnumerable<SelectListItem> GetSelectListItems(Type propertyType)
        {
            return Enum.GetNames(typeof(OrganizationTypeEnums)).ToList()
                .Select(c =>
                {
                    return new SelectListItem { Text = c, Value = c };
                });
        }
    }
public class UserTypeModel : CriterionModelBase
    {
        [DojoWidget(
         SelectionFactoryType = typeof(OrgTypeSelectionFactory),
         AdditionalOptions = "{ selectOnClick: true }"),
         Required]
        public string Type { get; set; }

        public override ICriterionModel Copy()
        {
            var model = (UserTypeModel)base.MemberwiseClone();
            model.Id = Identity.NewIdentity();
            return model;
        }
    }
#249814
Mar 10, 2021 9:32
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.