During a site redesign I was looking to add in a dynamic dropdown list of items. I would think this would be pretty straight forward in MVC, but there are some things to be aware of. I found this article from K.Scott Allen on OdetoCode.com. The article is extremely helpful in getting an ASP.Net MVC dropdownlistfor to work. One thing I did notice was an error in one line of the code. I am not sure if it is because of the version of MVC I am using or what but just wanted to put this out there just in case someone else runs into the same issue I had.
I followed the post to the letter, but was getting an error on the view.
@Html.DropDownListFor(m =>; m.SelectedFlavorID, Model.FlavorItem)
The issue was I needed to use something like this:
@Html.DropDownListFor(m => m.SelectedFlavorID, new SelectList(Model.FlavorItem, "Value", "Text"))
As you can see I needed to add the “new SelectList” to the parameter. Not really sure if this was an error or if it is something I am doing incorrectly in the server code. This took me a little bit to track down what the actual issue was. This may seem pretty simple for some, but want to put it out there for anyone who may have a similar issue.
Hope this helps.