超文本标记语言下拉-获取每个选项的值与文本相同
本文关键字:文本 选项 获取 超文本标记语言 | 更新日期: 2023-09-27 17:49:27
我想做一些简单的-我开始学习MVC,我有点困惑。我有一个下拉菜单,生成如下
<selectid="selectMenu" name="selectMenu">
<option value="Weekly">Weekly</option>
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Annually">Annually</option>
</select>
因此,我在我的视图中添加了以下内容-
<% string[] rate = new string[]{"Weekly","Monthly","Quarterly","Annually"}; %>
<%: Html.DropDownList("selectMenu", new SelectList(rate))%>
结果如下:
<select DataTextField="" DataValueField="" Items="System.String[]" SelectedValue="" SelectedValues="" id="selectMenu" name="selectMenu">
<option>Weekly</option>
<option>Monthly</option>
<option>Quarterly</option>
<option>Annually</option>
</select>
如何使每个选项的值与文本相同?
也许我应该坚持使用JavaScript?你可以只使用循环而不使用html帮助器(语法可能是关闭的)
<select id="selectMenu" name="selectMenu">
<% string[] rates = new string[]{"Weekly","Monthly","Quarterly","Annually"}; %>
<% foreach (string rate in rates) { %>
<option value="<%: rate %>"><%: rate %></option>
<% } %>
</select>
如果你刚刚开始,我建议使用asp.net-mvc3中的Razor视图引擎。语法是:
<select id="selectMenu" name="selectMenu">
@( string[] rates = new string[]{"Weekly","Monthly","Quarterly","Annually"}; )
@foreach (string rate in rates)
{
<option value="@rate">@rate</option>
}
</select>
<% var rate = new Dictionary<string, string> { { "Weekly", "Weekly" }, { "Monthly", "Monthly" }, { "Quarterly", "Quarterly" }, { "Annually", "Annually" } }; } %>
<%: Html.DropDownList("selectMenu", new SelectList(rate, "Key", "Value"), "--please select--") %>