【发布时间】:2019-05-07 21:32:34
【问题描述】:
我正在使用 ASP.NET MVC5,我正在尝试创建一个 EnumDropDownListFor where :
- 用户可以选择多个值(在一个填充有枚举值的下拉列表中)
- 提交表单时,选定的值将绑定到模型。
这是我迄今为止尝试过的:
@Html.ListBoxFor(m => m.SelectedHeatingTypes, new SelectList(Model.HeatingTypeItems, "Value", "Text"))
@Html.ListBoxFor(m => m.SelectedHeatingTypes, Model.HeatingTypeItems, new { @class = "form-control" })
@Html.ListBoxFor(m => m.SelectedHeatingTypes, new MultiSelectList(Model.HeatingTypeItems, "Value", "Text"))
@Html.EnumDropDownListFor(m => m.HeatingTypes, new { @class = "form-control selectpicker", @multiple = "multiple"})
@Html.ListBoxFor(m => m.HeatingTypes, Model.HeatingTypeItems, htmlAttributes: new { @class = "form-control", multiple = "multiple" })
@Html.ListBoxFor(m => m.HeatingTypes, new SelectList(Enum.GetValues(typeof(HeatingType))), new { @id = "ddlMyEnum", @multiple = "multiple" })
在我的模型上,我有这两个属性
public IEnumerable<int> SelectedHeatingTypes { get; set; }
public IEnumerable<SelectListItem> HeatingTypeItems { get; set; }
我尝试将IEnumerable<int> SelectedHeatingTypes 更改为int[],没有成功
如何在模型属性上绑定多选?我对 Arrays、Lists、IEnumerable 以及我可以在服务器端使用的所有内容都持开放态度......
【问题讨论】:
-
这里有一些答案stackoverflow.com/questions/21878673/…。也许可以帮助你。
-
这个链接解释了如何在 EnumDropDownListFor 中选择一个元素。这有效但不能回答我的问题,即:如何在下拉列表中选择多个元素,并将其与我的模型属性绑定
-
如果把这段代码sn -p
@multiple = "multiple"改成multiple = "true" -
我已经尝试过了(请参阅我的原始帖子中的示例),不幸的是它不起作用
标签: c# asp.net-mvc data-binding asp.net-mvc-5 bind