简体   繁体   English

在EnumDropDownListFor / ListBoxFor上绑定多个值

[英]Bind multiple values on EnumDropDownListFor / ListBoxFor

I'm using ASP.NET MVC5 and I'm trying to create an EnumDropDownListFor where : 我正在使用ASP.NET MVC5,我正在尝试创建一个EnumDropDownListFor ,其中:

  • User may select multiple values (in a drop down filled with enum values) 用户可以选择多个值(在下拉列表中填充枚举值)
  • The selected values are binded to the model when the form is submitted. 提交表单时,所选值将绑定到模型。

Here is what I tried so far : 这是我到目前为止尝试的内容:

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

On my model, I have thiese two properties 在我的模型上,我有两个属性

public IEnumerable<int> SelectedHeatingTypes { get; set; }
public IEnumerable<SelectListItem> HeatingTypeItems { get; set; }

I tried to change the IEnumerable<int> SelectedHeatingTypes to int[] , no more success 我试图将IEnumerable<int> SelectedHeatingTypes更改为int[] ,不再成功

How can I bind the multi selection on a model property ? 如何在模型属性上绑定多选? I'm open to Arrays, Lists, IEnumerable<>, everything I can work with server side... 我对阵列,列表,IEnumerable <>开放,我可以与服务器端一起工作......

I would check out KnockoutJs as that may solve your issue. 我会查看KnockoutJs,因为这可以解决你的问题。 That is a client-side binding, though, so you'd need to get your model to the client and then use KO to bind the data to your UI. 这是一个客户端绑定,因此您需要将模型提供给客户端,然后使用KO将数据绑定到UI。

https://knockoutjs.com/documentation/selectedOptions-binding.html https://knockoutjs.com/documentation/selectedOptions-binding.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM