I'm trying to populate a DropDownList
and setting the default value of it to the first element of the list, however the dropdown
gets populated but as much as i try with different approaches, I cant set the default value. I have a controller method that creates the selectlist
and pass it to the view in the viewbag
like this:
List<society> societyList= TiposCollection.getSociety();
List<SelectListItem> items = new List<SelectListItem>();
foreach (society societyItem in societyList)
{
SelectListItem item = new SelectListItem
{
Text = society.nombre,
Value = society.id.ToString(),
};
items.Add(item);
}
items.FirstOrDefault().Selected = true;
ViewBag.sociedad_emisora = new SelectList(items);
And in the view I create the DropDownList
in this way:
@Html.DropDownListFor(x => x.ov.sociedad_emisora,
(SelectList)ViewBag.sociedad_emisora,
"--sociedad emisora--",
new { @class = "input-lm form-control"})
The dropdown
gets populated but there is no selected vale. Any suggestion?