1

I'm trying to implement an MVC webpage, that contains a dropdown for enums. It works fine, when bound to a single-valued property, like MyEnum MyProperty {get;set;} Now, I'd like to turn it into a multiselect list - but in this case @Html.EnumDropDownListFor() fails with the following exception:

Return type 'System.Collections.Generic.List'1[[MyNamespace, Version=1.0.0.0, Culture=neutral, MyEnum=null]]' is not supported. Parameter name: expression'

A similar thing for string collections works fine. Any ideas?

Model

public IEnumerable StringValues;

[Display(Name = "Works fine for strings")]
public IEnumerable<string> StringCollectionProperty { get; set; }

[Display(Name = "Fails for enums")]
[EnumDataType(typeof(MyEnum))]
public IEnumerable<ProcessingStatus> MyEnumCollectionProperty { get; set; }

View

// This works fine
@Html.DropDownListFor(model => model.StringCollectionProperty, Model.StringValues, htmlAttributes: new { @class = "form-control", multiple = "multiple" })

// This fails with the error above
@Html.EnumDropDownListFor(model => model.MyEnumCollectionProperty, "Please select...", htmlAttributes: new { @class = "form-control", multiple = "multiple" })
balintn
  • 988
  • 1
  • 9
  • 19
  • 1
    The first does not actually work fine - it needs to be `ListBoxFor()` as explained [here](https://stackoverflow.com/questions/40725358/why-does-the-dropdownlistfor-lose-the-multiple-selection-after-submit-but-the-li/40732481#40732481). –  Nov 16 '17 at 12:25
  • What is typeof `ProcessingStatus`? –  Nov 16 '17 at 12:27

0 Answers0