So, I have been bitten by this error roughly three times now and every time I forget what it was that I did to fix it. So this time, I thought I would blog about it and maybe it would stick this time and at the same time maybe help someone else out with this error.
I think the reason the fix for this error is so easily forgotten is because the error itself has absolutely nothing to do with the above message. Every time I have seem this error, ultimately the problem was related to the misuse of the SelectList HTML helper. Here is how the select list is supposed to work.
@Html.SelectList(model => model.SelectedItem, Model.SelectList);
So what this is doing is using the model’s SelectedItem property (custom property you create) as the slot to place the item the user selects from the list. The second paramater is the list of items that the user should choose from in the form of a SelectList. This is a big key right here. You must use a SelecList type for this list or something that you can cast as a selectList in the view itself (not recommended). The reason I don’t recommend this is because things like this can fail and it is much easier to recover from a bad cast in code that it is once the View is being put together.
Take care to make sure that the SelectList is in the proper format and is indeed of type SelectList and not some other type that you think is of type SelectList. if you need any help with converting a SelectList generically to something else, feel free to ask, or see my post on converting Enums to Selectlists here to get you started.
I hope this helps other people from following a similar rabbit hole.