【发布时间】:2017-04-04 15:26:10
【问题描述】:
我有一个表单,用户可以在其中添加某种类别并向这些类别添加一些选项。
当用户输入类别及其项目的数据时,我将它们动态添加到<select> 中,其中类别为<optgroup>,因此我可以将其绑定到某个 ViewModel 属性,用户输入后看起来像这样数据:
<select asp-for="Items">
<optgroup label="Brands">
<option value="Brand1">Brand1</option>
<option value="Brand2">Brand2</option>
<option value="Brand3">Brand3</option>
<option value="Brand4">Brand4</option>
</optgroup>
<optgroup label="Something">
<option value="Something1" selected>Something1</option>
<option value="Something2" selected>Something2</option>
<option value="Something3" selected>Something3</option>
<option value="Something4" selected>Something4</option>
</optgroup>
</select>
然后我尝试将它绑定到 ViewModel 中的 Items 属性,但我可以绑定它的唯一方法是使用 List<string> 类型的 Items。
这样我就失去了每个选项所属的组。
我如何将它绑定到 Dictionary<string, List<string>> 之类的东西上,我可以在其中保留我在 html 上所做的分组?
PS.:选择将是不可见的,它只是我以某种结构获取数据的一种方式,我可以在表单中提交。演示文稿将在某种标签中完成。 喜欢:
【问题讨论】:
-
你不能。
<select>仅回发其选定选项的值(并绑定到IEnumerable<string>)。请求中未发送与其<optgroup>相关的任何内容。 -
除了让每个选项的
value="brand__brandname"并在控制器上处理之外,没有别的办法了吗? -
简答 - 否。
标签: c# asp.net asp.net-mvc razor asp.net-core