Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

asp.net mvc - What HTML helper do I use to create a simple dropdownlist that doesn't take in any variables?

I want to have a simple select->option dropdown list that I am not passing any (SelectItem collection) values to. I already know the values so I don't need to do all that (they are static).

Need to do something like so:

<select id="day" name="day">
  <option value="1">Sunday</option>
  <option value="2">Monday</option>
</select>

<select id="hour" name="hour">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select>

All examples seem to show how to create a IEnum passing it in via the ViewData. This is in a partial, and I don't want to be sending in this data, I just want it to show up.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use a select list with either a List of strings or a Dictionary of items (if you want different id's and values) inside your drop down list to define your values.

<%= Html.DropDownList("day", new SelectList(
    new Dictionary<int,string> { { 1, "Sunday" }, { 2, "Monday" } },
    "Key", "Value"))
%>

<%= Html.DropDownList("hour", new SelectList(
    new List<string>() { "1", "2", "3", "4" }))
%>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...