Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Enum selection factory not working – what is the problem?

Vote:
 

I need to use System.Net.Mail.MailPriority as enum on property. I have found many recepies how to do it but none of them worked.

Code:

public class EmailPrioritySelectionFactory : ISelectionFactory
{
  public IEnumerable  GetSelections(ExtendedMetadata metadata)
        {
            return new[]
            {
                new SelectItem { Text = "Low", Value = MailPriority.Low },
                new SelectItem { Text = "Normal", Value = MailPriority.Normal },
                new SelectItem { Text = "High", Value = MailPriority.High },
            };
        }
}

[EditorDescriptorRegistration(TargetType = typeof(MailPriority))]
public class EmailPriorityEditorDescriptor : EditorDescriptor
{
        public EmailPriorityEditorDescriptor()
        {
            SelectionFactoryType = typeof(Business.SelectionFactories.CustomerServicePage.EmailPrioritySelectionFactory);
            ClientEditingClass = "epi-cms/contentediting/editors/SelectionEditor";
            DefaultValue = MailPriority.Low;
        }
}


Issue is that default value from editor descriptor is not mapped in any case. It takes 0 – normal. I am affraid I have found bug.

On setting

[EditorDescriptor(EditorDescriptorType = typeof(EditorDescriptors.EmailPriorityEditorDescriptor))]
[BackingType(typeof(PropertyNumber))]
public virtual MailPriority Priority { get; set; }

or

[SelectOne(SelectionFactoryType = typeof(EmailPrioritySelectionFactory))]
[BackingType(typeof(PropertyNumber))]
public virtual MailPriority Priority { get; set; }

on value change I got (XHR) response from server

message: "Unable to load /EPiServer/cms/Stores/contentdata/35_75 status: 500"
​
response: Object { url: "/EPiServer/cms/Stores/contentdata/35_75", loaded: 9009, total: 9009, … }
​
stack: "_317@https://abcXYZsite.local/EPiServer/Shell/11.4.4/ClientResources/dojo/dojo.js:15:78142\n_4f2@https://abcXYZsite.local/EPiServer/Shell/11.4.4/ClientResources/dojo/dojo.js:15:133806\n_4fe@https://abcXYZsite.local/EPiServer/Shell/11.4.4/ClientResources/dojo/dojo.js:15:134081\n"
​
status: 500
[InvalidCastException: Specified cast is not valid.]
   Castle.Proxies.XYZabcProxy.get_Priority() +204

[TargetInvocationException: Property accessor 'Priority' on object 'Castle.Proxies.XYZabcProxy' threw the following exception:'Specified cast is not valid.']
   System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +490
   EPiServer.Validation.DataAnnotationsValidator`1.ValidateInstance(Object instance, List`1 validationResult, ValidationContext validationContext) +618
   EPiServer.Validation.DataAnnotationsValidator`1.Validate(T instance) +183
   EPiServer.Validation.Internal.ContextValidatorWrapper`2.Validate(Object instance) +103
   EPiServer.Validation.Internal.ValidationService.ValidateRecursively(Object instance, Object context, HashSet`1 visitedInstances) +442
   EPiServer.Cms.Shell.UI.Rest.ContentChangeManager.UpdateContentProperties(ContentReference contentReference, IDictionary`2 properties, SaveAction saveAction) +796
   EPiServer.Cms.Shell.UI.Rest.Internal.ContentDataStore.Patch(PatchContentDataModel entity) +357
   lambda_method(Closure , ControllerBase , Object[] ) +139
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +229
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +35
   System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +77
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +72
   System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +387
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +38
   System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +188
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
   EPiServer.Shell.Services.Rest.RestControllerBase.EndExecute(IAsyncResult asyncResult) +60
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +602
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128</></></></>

EPiServer.CMS.Shell.UI 11.4.4.0.

Am I doing something wrong in order to setup this simple enum selection on model?

#197842
Edited, Oct 15, 2018 17:11
Vote:
 

Hi Jiri,

The easy answer (probably) is to set it on your content type using:

public override void SetDefaultValues(ContentType contentType)
{
    base.SetDefaultValues(contentType);

    Priority = MailPriority.Low,
}

Do you have any reason not to use this approach?

/Jake

#197850
Oct 15, 2018 18:36
Vote:
 
#197851
Oct 15, 2018 18:38
Vote:
 

Ah, ok.

In Admin > Content Type can you navigate to a content type that is using your Priority property. What type is it? I'm expecting integer from your code above.

#197852
Oct 15, 2018 18:44
Vote:
 

It’s Long string :-( but you intention was right. Setting it to int fixed the problem. I spent with it some hours. Thank you.

#197853
Edited, Oct 15, 2018 18:45
Vote:
 

Hitting the "Revert to Default" button should fix that -- if I remember correctly.

#197854
Oct 15, 2018 18:49
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.