开发者

Html.Dropdown - get the value of each option to be the same as the text? [duplicate]

开发者 https://www.devze.com 2023-02-22 23:27 出处:网络
This question already has answers here: What HTML helper do I use to create a simple dropdownlist that doesn't take in any variables?
This question already has answers here: What HTML helper do I use to create a simple dropdownlist that doesn't take in any variables? (2 answers) 开发者_如何学JAVA Closed 2 years ago.

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--") %>
0

精彩评论

暂无评论...
验证码 换一张
取 消