Multiselect MVC5.2 Html.EnumDropDownListFor()在绑定到集合属性时失败

时间:2017-11-16 12:20:01

标签: asp.net-mvc html.dropdownlistfor

我正在尝试实现一个MVC网页,其中包含枚举下拉列表。 它绑定到单值属性时工作正常,如MyEnum MyProperty {get;set;} 现在,我想将其转换为多选列表 - 但在这种情况下,@Html.EnumDropDownListFor()失败并出现以下异常:

  

不支持返回类型'System.Collections.Generic.List'1 [[MyNamespace,Version = 1.0.0.0,Culture = neutral,MyEnum = null]]'。   参数名称:表达式'

字符串集合的类似工作正常。 有什么想法吗?

模型

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; }

查看

// 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" })

0 个答案:

没有答案