ASP.NET MVC DropDownListFor not selecting value from model

The reason why this doesn’t work is because of a limitation of the DropDownListFor helper: it is able to infer the selected value using the lambda expression passed as first argument only if this lambda expression is a simple property access expression. For example this doesn’t work with array indexer access expressions which is your case because of the editor template.

You basically have (excluding the editor template):

@Html.DropDownListFor(
    m => m.ShippingTypes[i].RequiredShippingTypeId, 
    ViewBag.ShippingTypes as IEnumerable<SelectListItem>
)

The following is not supported: m => m.ShippingTypes[i].RequiredShippingTypeId. It works only with simple property access expressions but not with indexed collection access.

The workaround you have found is the correct way to solve this problem, by explicitly passing the selected value when building the SelectList.

Leave a Comment