Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC looping in razor, generated dropdownlist not value selected

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?

like image 896
Manuel Valle Avatar asked Jun 29 '12 21:06

Manuel Valle


1 Answers

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.

like image 77
LukLed Avatar answered Sep 24 '22 02:09

LukLed