1

I am trying to create a dropdown list that contains values from my data model. I cannot seem to get this to work inside of my view:

@Html.DropDownList("Program Types", 
new SelectList(Model.Select(x => x.ProgramType.ProgramType).Distinct().OrderBy(x => x),
Model.Select(x => x.ProgramType.ProgramTypeId).Distinct().OrderBy(x => x)))
user547794
  • 14,263
  • 36
  • 103
  • 152
  • 6
    What do you mean by cannot get it to work, do you get exception or does the dropdownlist does not appear at all – HatSoft Aug 24 '12 at 20:33
  • 3
    WOW, have you considered using view models? If you haven't then, well, have :-) When I see this code in a view I feel the need to drink beer. – Darin Dimitrov Aug 24 '12 at 20:36
  • You are using `new SelectList` incorrectly. You are passing in more then one item to the `object` parameter of the constructor. It is meant to have a SINGLE object passed to it and that object is what is considered the `selected` item in the drop down list. – Jared Aug 24 '12 at 20:37
  • Support beer drinking! :) And, yes, use view models. Make life so much easier – Display Name Aug 24 '12 at 23:00

1 Answers1

2

Take a look at this example:

How to write a simple Html.DropDownListFor()

It doesn't get any simpler than that and you can adjust this example to your case quite easily.

Hope this is of help to you & good luck!

Community
  • 1
  • 1
Display Name
  • 4,672
  • 1
  • 33
  • 43