views:

137

answers:

2

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.

+1  A: 

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" }))
%>
David Liddle
For some reason SelectList and the System.Web.Mvc namespace doesn't show up under intellesense. I'll have to give it a try and see if it's just an intellesense problem or if that'll work. For some stupid reason I just assumed that that wouldn't work.
rball
Sometimes intellisense for me stops working but can't help you there. Also, as jfar said, if you have static values just use HTML instead of a helper.
David Liddle
Is there a way to keep the state of the drop downs without using a helper?
rball
A: 

If they are static keep them as HTML. No sense complicating things.

jfar
Problem is when you have validation on a form, when you return to the form after an error, the state of the drop downs are lost. With helpers they remain.
rball
Ok, sounded like you didn't need any ModelState info.
jfar
I don't want to have to pass the drop down values themselves in, but I do want the state of the boxes to remain after a round trip.
rball