Try our conversational search powered by Generative AI!

Custom Visitor Group enum dropdown not populating

Vote:
 

Hello everyone

I'm building a custom visitor group but the match criteria drop down doesn't populate from an enum. I'm coding as the following

[EPiServerDataStore(AutomaticallyRemapStore=true)]
    public class SearchParameterModel : CriterionModelBase
    {
        [Required, DojoWidget(SelectionFactoryType = typeof(EnumSelectionFactory))]
        public SearchValueCondition Condition { get; set; }

        [Required]
        public string Value { get; set; }

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

    public enum SearchValueCondition
    {
        Match,
        StartsWith
    }

but the dojowidget doesn't pick up the enum and i can't see why and I can't see how to debug further. 

If a create a custom selection factory like this

    public class CustomSelectionFactory : ISelectionFactory
    {
        IEnumerable<SelectListItem> ISelectionFactory.GetSelectListItems(Type property)
        {
            return Enum.GetNames(property).Select(n => new SelectListItem { Text = n, Value = n });
        }
    }

Then it will populate (horray) but when I go in to edit a saved criteria then the item the user selected is lost and needs to be readded (booooo). If I could get either one working then I'd be happy but at the moment neither works fully

As anyone got any advice/tips . As always all help sis gratefully received

Many Thanks.

#77864
Nov 27, 2013 13:24
Vote:
 

Hi Tim,

First thing I can say with your version 1 is: You will get missing text (because in EnumSelectionFactory will use LocalizationService to get text for each value)

2nd version should cast value to int like this:

return new SelectListItem
                {
                    Text = n,
                    Value = ((int)n).ToString()
                };

 

 

Hope that help!

Ha Bui

    

#77885
Nov 28, 2013 7:55
Vote:
 

Thanks you for that. Unfortunately the code won't compile so I changed to 

 return Enum.GetNames(property).Select(n => new SelectListItem
            {
                Text = n,
                Value = Convert.ToInt32(n).ToString()
            });

But this causes the visitor group to crash with 'input string is not in correct format'

With the first version - the localisation service should pick up from an aml file in language. But i have one with the below format

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<languages>
    <language name="en" id="en">
        <shell>
            <cms>
                <visitorgroups>
                    <criteria>
                        <search>
                            <key>Term:</key>
                        </search>
                    </criteria>
                </visitorgroups>
            </cms>
        </shell>
		
		<enums>
			<criteriapack>
				<searchcriterion>
					<searchvaluecondition>
						    <match>Match</match>
                <startswith>Starts with</startswith>
          </searchvaluecondition>
				</searchcriterion>
			</criteriapack>
		</enums>
  </language>
</languages>

Is that wrong?

Any other thoughts would be really appreciated

#77911
Edited, Nov 28, 2013 11:13
Vote:
 

Actually i don;t need this to be perfect so I have just associated it with the actual instance of the enum. Works fine but won't win prizes

    public class SearchValueSelectionFactory : ISelectionFactory
    {
        IEnumerable<SelectListItem> ISelectionFactory.GetSelectListItems(Type property)
        {
            return new List<SelectListItem>
            {
                new SelectListItem
                {
                    Text = "Match",
                    Value = ((int)SearchValueCondition.Match).ToString()
                },
                new SelectListItem
                {
                    Text = "Starts With",
                    Value = ((int)SearchValueCondition.StartsWith).ToString()
                }
            };
        }
    }

Cheers

#77917
Nov 28, 2013 11:41
Vote:
 

Hi Tim,

Why you don't keep calm and write down correct structure in your xml lang file?

Because you just follow by error message (Missing Text ...) is enough!

<?xml version="1.0" encoding="utf-8" ?>
<languages>
  <language name="English" id="en">
    <enums>
      <episerver>
        <templates>
          <alloy>
            <models>
              <searchvaluecondition>
                <match>Match</match>
                <startswith>Start With</startswith>
              </searchvaluecondition>
            </models>
          </alloy>
        </templates>
      </episerver>
    </enums>
  </language>
</languages>

    

Any way, if you just want to show it then ok! ;)

BR

Ha Bui

#77946
Nov 29, 2013 4:03
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.