-2

Here is my code:

<%: Html.DropDownListFor(m => m.type, new SelectList(new[] { "something", "other", "third thing" }))%>

And m.type has a value of 1, which means that "other " should be selected. But it isn't. Why is this happening and how to solve it?

petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

1 Answers1

1

The reason this isnt working is because the select list items dont have a numerical value. If you look at your source code you will probably see that the value is the same as the text and not a number.

To fix this you need to build the select list adding in the numerical values as well.

So you may need to build up your select list like this:

 new SelectList(new[] { 
            new SelectListItem{Text = "something", Value = 0}, 
            new SelectListItem{Text ="other", Value = 1},
            new SelectListItem{Text ="third thing", Value = 2 }})
Gaz Winter
  • 2,924
  • 2
  • 25
  • 47