In my ViewModel i got two fields :
public AdTypeEnum Type { get; set; }
public IList<SelectListItem> TypeEnumList { get; set; }
That's how i fill the list :
public void SetLists()
{
TypeEnumList = new List<SelectListItem>();
TypeEnumList.Add(new SelectListItem { Value = "0", Text = "Select the type"});
foreach (var en in Enum.GetValues(typeof(AdTypeEnum)).Cast<AdTypeEnum>())
{
TypeEnumList.Add(new SelectListItem
{
Value = ((int)en).ToString(),
Text = en.ToString(),
Selected = Type == (AdTypeEnum)en ? true : false
});
}
}
And then I'm just rendering a dropdown on my View :
@Html.DropDownListFor(x => x.Type, Model.TypeEnumList, new { @class = "form-control" })
But the selected value doesn't render and the first option is always selected. When I check the select in HTML I found that no one of the options got selected attribute, but when I debug my controller method I can see that always one of the selectListItem have the propety Selected=true. Why does it suddenly disappear when my view is rendering?