我有一個場景,我想從複選框列表中選擇3個項目。這工作正常,但如果複選框的列表變長,該頁面看起來很笨重。所以我想把它改成3個下拉列表。所以頁面要小很多,但是沒有一個下拉列表會兌現選定的值。將數組綁定到多個DropDownListFor
所以,我想這樣的代碼
@Html.DropDownListFor(model => model.CheckedLarcenyTypes, new SelectList(Model.LarcenyTypes, "Key", "Value", Model.CheckedLarcenyTypes.Length > 0 ? Model.CheckedLarcenyTypes[0] : null as Int32?), String.Empty, new { id = "Larceny1" })
@Html.DropDownListFor(model => model.CheckedLarcenyTypes, new SelectList(Model.LarcenyTypes, "Key", "Value", Model.CheckedLarcenyTypes.Length > 1 ? Model.CheckedLarcenyTypes[1] : null as Int32?), String.Empty, new { id = "Larceny2" })
@Html.DropDownListFor(model => model.CheckedLarcenyTypes, new SelectList(Model.LarcenyTypes, "Key", "Value", Model.CheckedLarcenyTypes.Length > 2 ? Model.CheckedLarcenyTypes[2] : null as Int32?), String.Empty, new { id = "Larceny3" })
現在的下拉列表中正確創建和正確的價值得到約束,並在文章中,我看到的視圖模型的價值越來越被送回。
我只是不能得到selectvalue顯示在下拉列表中。在重新加載頁面時,下拉菜單仍然是空白的。
我在這裏做錯了什麼?這甚至有可能嗎?
嗨弗蘭,如果列表很長,爲什麼不考慮一個替代解決方案,比如實現一個jQuery選擇列表插件就像在http://loopj.com/jquery-tokeninput/中找到的一樣,看看它是否會滿足你的情況?與複選框列表不同,這將顯示一個下拉列表,您可以從中選取條目。您選擇的每個條目將在文本區域內列出 –