I just want to select the first item by default but the drop down list has a blank item selected.
How I can resolve it? Thank you!
[Authorize]
public ActionResult Add()
{
var sportList = new SelectList(db.Sports.OrderBy(s => s.Name).ToList(), "ID", "Name");
UserTournamentModel m = new UserTournamentModel();
m.SportList = sportList;
m.SportID = Guid.Parse(sportList.FirstOrDefault().Value);
return View(m);
}
and here
<div class="editor-label" style="width: 70px;">
@Html.DropDownListFor(x => x.SportID, Model.SportList)
</div>
Use the
SelectListconstructor overload that accepts the selected item: