May be I just don’t know how to google, but I am trying to find an example where a dropdown control is data-binded to a collection (this part is working for me) and then I want to read the drop down selection back into a variable. This variable could be a property on the mode. Binding dropdown selected value back into variable is where I am having troube. In web-forms world this was no big deal but for some reason I cannot make it work with MVC 3. Any code samples or examples will be highly appreciated.
Share
The recommended way to do this in ASP.NET MVC is to use a view model. When you design a dropdownlist you need 2 properties on this view model: one that will be used to bind the selected value to and a second one which represents a collection of values and texts which will be used to generate the options. Then you could use the first property to preselect a given option.
So we start by designing view model:
then a controller action which populate this view model:
and in the strongly typed view use the
DropDownListForhelper to generate the dropdown: