I am using VS 2010 with MVC 3 and EF 5. I am using a common pattern for dropdown lists that works correctly in all but one instance and I cannot see why this one is failing to select the correct entry in the select list. The following are code snippets.
The select list is created as follows:
public static IEnumerable<SelectListItem> GetOutcomes()
{
CodesEntities dataContextCodes = new CodesEntities(ConnectionString);
return new SelectList(dataContextCodes.CodeOutcome.
Where(x => x.DisplayOrder > 0).OrderBy(x => x.DisplayOrder),
"OutcomeCodeID", "Outcome");
}
This returns the correct select list.
The view has the following code:
@Html.DropDownListFor(m => m.OutcomeCodeID,
PerintalFormViewModels.GetOutcomes(), "Please select an item")
The model value m.OutcomeCodeID has a valid value (1) but no item is being selected.
The generated HTML is:
<select id="CodeID" name="OutcomeCodeID" data-val-required="Outcome is required" data-val-number="The field outcome must be a number." data-val="true">
<option value="">Please select an item</option>
<option value="1">Termination</option>
<option value="2">Loss</option>
<option value="3">Still</option>
<option value="4">Live</option>
</select>
I am in the hair tearing out, being driven nuts stage. Does anyone have any thoughts?
Thanks
You can do it like this:
Or this:
Then call it like this: