2017-04-04 108 views
0

我有一个表单,用户可以添加某种类别并向这些类别添加一些选项。 当用户输入类别及其项目的数据时,我将它们动态添加到<select>中,其中类别为<optgroup>,因此我可以将它绑定到某个ViewModel属性,在用户输入数据后它看起来像这样:绑定<select>在ASP.Net中有多个选项MVC

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

然后我尝试这个绑定到视图模型的Items属性,但我可以绑定这个问题的唯一办法,就是用型List<string>Items。 这样我就失去了每个选项所属的组。 我怎么能把它绑定到像Dictionary<string, List<string>>这样的地方,我可以保留我在html上做的分组?

PS .:选择将是不可见的,它是我获取某些结构中数据的唯一方式,我可以通过表单提交。演示文稿将在某种标签中完成。 像:

enter image description here

+0

你不能。一个'