Don*_* V. 4 html c# asp.net-mvc
我有这个HTML ...
<select id="View" name="View">
<option value="1">With issue covers</option>
<option value="0">No issue covers</option>
</select>
Run Code Online (Sandbox Code Playgroud)
它不会让我插入这样的代码......
<select id="View" name="View">
<option value="1" <% ..logic code..%> >With issue covers</option>
<option value="0" <% ..logic code..%> >No issue covers</option>
</select>
Run Code Online (Sandbox Code Playgroud)
那么设置一个被选中的最佳方式是什么?
更新:不使用HTML帮助程序.
"最佳"方法可能是使用助手:
var selectList = new SelectList(data, "ValueProp", "TextProp", data[1].ValueProp);
... Html.DropDownList("foo", selectList)
Run Code Online (Sandbox Code Playgroud)
"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));
Run Code Online (Sandbox Code Playgroud)
另一种方法可能是通过脚本选择正确的客户端项目,但显然只能在启用脚本的情况下使用.
请注意,数据声明中缺少逗号和分号,使其停止工作