我有一个场景,我想从复选框列表中选择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/中找到的一样,看看它是否会满足你的情况?与复选框列表不同,这将显示一个下拉列表,您可以从中选取条目。您选择的每个条目将在文本区域内列出 –