Question

I am trying to make a dropdown list using DropdownListFor function and I want to have a value selected in it.

I have searched a lot and found the similar kind of solution of having a SelectList object in the ViewModel, I tried it something like this

In my ViewModel I used a property of type SelectList like this

SelectList HoursToSelect = new SelectList(MyDictionaryObject, "Value", "Key", 14/*selected value*/);

and I was using it like this

@Html.DropDownListFor(m => m.EndsOnHour, HoursToSelect)

This all was working fine and fulfilled my requirement completely

But the problem is due to my project's architecture I cannot use SelectList object in my ViewModel as my project does not allow non serializable classe in the ViewModel.

So the only way left for me is to have something in my View only, something here

 @Html.DropDownListFor(m => m.EndsOnHour, new SelectList(Model.Hours, "Value", "Key"))

But don't know what! Anybody have idea to achieve this?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top