我正在尝试做一些简单的事情 - 我开始学习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?
答案 0 :(得分:0)
答案 1 :(得分:0)
<% var rate = new Dictionary<string, string> { { "Weekly", "Weekly" }, { "Monthly", "Monthly" }, { "Quarterly", "Quarterly" }, { "Annually", "Annually" } }; } %>
<%: Html.DropDownList("selectMenu", new SelectList(rate, "Key", "Value"), "--please select--") %>
答案 2 :(得分:0)
您可以使用循环而不使用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 View引擎。语法如下:
<select id="selectMenu" name="selectMenu">
@( string[] rates = new string[]{"Weekly","Monthly","Quarterly","Annually"}; )
@foreach (string rate in rates)
{
<option value="@rate">@rate</option>
}
</select>