tags:

views:

443

answers:

2

I'm using the DropDownListFor helper method inside of an edit page and I'm not having any luck getting it to select the value that I specify. I noticed a similar question on Stackoverflow. The suggested workaround was to, "populate your SelectList in the view code". The problem is that I've already tried this and it's still not working.

<%= Html.DropDownListFor(model => model.States, new SelectList(Model.States.OrderBy(s => s.StateAbbr), "StateAbbr", "StateName", Model.AddressStateAbbr), "-- Select State --")%>

I have set a breakpoint and have verified the existence (and validity) of model.AddressStateAbbr. I'm just not sure what I'm missing.

+4  A: 

Try:

<%= Html.DropDownListFor(model => model.AddressStateAbbr, new SelectList(Model.States.OrderBy(s => s.StateAbbr), "StateAbbr", "StateName", Model.AddressStateAbbr), "-- Select State --")%>

The expression based helpers don't seem to respect the Selected property of the SelectListItems in your SelectList.

BnWasteland
That worked! Thank you very much!
senfo
A: 

This works beautifully. Now, what do I do for null values?

Amanda Earlam