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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…