Razor MVC loop generated by dropdownlist not selected

I have a loop in a razor that generates a pattern several times. Inside the template is a drop-down list

@Html.DropDownlistFor(x=>x.pasajero[i].option, Model.optionItems)

A drop-down list is displayed, but the option is selected="selected"not set.

I was able to correctly display the dropdown outside the loop using the same values. Any ideas why?

+5
source share
1 answer

Try:

@Html.DropDownlistFor(x => x.pasajero[i].option,new SelectList(Model.optionItems,"IdField","DisplayField", Model.pasajero[i].option))

Be sure to x.pasajero[i].optionhave the same type as IdField.

+8
source

All Articles