I'm trying to do the simplest thing with an dropdown and it's just not working. I have a an integer property called SearchCriteria.Distance. It's a simple integer property. I'm trying to bind that property to a drowpdown list of integers but it will not bind. The value is always 0. Here's the code:
@Html.LabelFor(x => x.SearchCriteria.Distance, "Radius (miles)", new { @class="control-label" })
<div class="controls">
@Html.DropDownListFor(x => x.SearchCriteria.Distance, new SelectList(new int[] { 5, 15, 25, 50 }), new { @class="input-small", style="height:36px;"})
</div>
Because it's a simple integer list, there's not Text Value to associate it with. What am I doing wrong here?
Edit: Turns out this problem was the result of a stupid error on my part. I had a hidden field with the SearchCriteria.Distance id on my form that I forgot about that prevented the drop down value from being set. I marked the solution below as the answer because it is correct.
DropDownListForhelper will always render out a<select>list in HTML, which only takes string values. Your best bet is to create a newIEnumerable<SelectListItem>in a view-model, then pass this into your view and then reference it in yourDropDownListFor.DropDownListForaccepts anIEnumerableof items and the OP's code will work -new SelectList(new int[] { 5, 15, 25, 50 }). @DonFitz can you post your controller code and view.