MVC looping in razor, generated dropdownlist not v

2019-06-19 02:23发布

问题:

I have a loop in razor which generates a template a number of times. Inside the template there is a dropdownlist

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

The dropdownlist is rendered, however no option is set with the selected="selected" attribute.

I have been able to properly render the dropdownlist outside a loop using same values. Any ideas why?

回答1:

Try:

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

Make sure x.pasajero[i].option has the same type as IdField.