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)))
4
  • 6
    What do you mean by cannot get it to work, do you get exception or does the dropdownlist does not appear at all Commented Aug 24, 2012 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. Commented Aug 24, 2012 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. Commented Aug 24, 2012 at 20:37
  • Support beer drinking! :) And, yes, use view models. Make life so much easier Commented Aug 24, 2012 at 23:00

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.