I'm trying to do something simple - I'm starting to learn MVC and I'm getting a tad confused. I have a dropdown that produces the following
<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>
thus I added the following to my view -
<% string[] rate = new string[]{"Weekly","Monthly","Quarterly","Annually"}; %>
<%: Html.DropDownList("selectMenu", new SelectList(rate))%>
However this produced the following:
<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>
How do I get the value of each option to be the same as the text?
Perhaps I should stick to JavaScript?
You could just use a loop and not use a html helper (syntax might be off)
<select id="selectMenu" name="selectMenu">
<% string[] rates = new string[]{"Weekly","Monthly","Quarterly","Annually"}; %>
<% foreach (string rate in rates) { %>
<option value="<%: rate %>"><%: rate %></option>
<% } %>
</select>
If your just starting out, I'd recommend using the Razor View engine in asp.net-mvc3. The syntax would be:
<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--") %>
精彩评论