2017-04-04 248 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

你不能。一個'