在 ASP.NET MVC 标记中设置下拉列表以进行选择的最佳方法是什么?

2023-12-13

我有这个html...

<select id="View" name="View">
   <option value="1">With issue covers</option>
   <option value="0">No issue covers</option>
 </select>

它不会让我插入这样的代码......

<select id="View" name="View">
   <option value="1" <% ..logic code..%> >With issue covers</option>
   <option value="0" <% ..logic code..%> >No issue covers</option>
 </select>

那么将其设置为选定的最佳方法是什么?

更新: 不使用 HTML 帮助程序。


“最好的”方法可能是use帮助者:

var selectList = new SelectList(data, "ValueProp", "TextProp", data[1].ValueProp);
... Html.DropDownList("foo", selectList)

其中“data”可以是匿名类型的数组,例如:

var data = new[] {
  new {Key=1, Text="With issue covers"},
  new {Key=0, Text="No issue covers"}
};
// todo: pick the selected index or value based on your logic
var selectList = new SelectList(data, "Key", "Text", data[1].Key);
Writer.Write(Html.DropDownList("foo", selectList));

另一种方法可能是通过脚本在客户端选择正确的项目,但显然这只适用于启用脚本的情况。

请注意,数据声明中缺少逗号和分号,导致其无法工作

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 ASP.NET MVC 标记中设置下拉列表以进行选择的最佳方法是什么? 的相关文章

随机推荐