I got a question concerning a DropDownList with a selected value.
@Html.DropDownListFor(m => m.weekForEachDayService[6].serviceId, new SelectList (ViewBag.DayService, "Value", "Text"))
As you can see I have a service that is related to a week with a table weekForEachService.
When I run this it never uses the serviceId inside the SelectList.
@Html.DropDownListFor(m => m.weekForEachDayService[6].serviceId, new SelectList (ViewBag.DayService, "Value", "Text",m => m.weekForEachDayService[6].serviceId))
I also tried something like this but then I get the Error message cannot convert type lambda to object. Usually when I use a DropDownList it automatically selects the correct value, but with this related table I think it’s necessary to put the selected value in the list.
Thanks you for helping
Since the lambda expression that you are using is complex and involves array indexers the
DropDownListForhelper cannot determine the selected value. It only works with simple property access lambda expressions.In this case you could pass the selected value as 4th argument to the
SelectListconstructor, like this: