Skip to content

23 DropDownList Options

M. Fares edited this page Apr 6, 2018 · 1 revision
  1. To order the items in a dropdown list

In the action, order the list according to the name/value.

public ActionResult Create()
{
    ViewBag.DepartmentId = new SelectList(db.Departments.OrderBy(n => n.Name), "Id", "Name");
    return View();
}
  1. To replace a white space element in the dropdown list when the property is nullable or add an item to the dropdown list
@Html.EnumDropDownListFor(model => model.Level, "Select Faculty Level", htmlAttributes: new { @class = "form-control" })

@Html.DropDownList("DepartmentId", null, "Select Department", htmlAttributes: new { @class = "form-control" })

Clone this wiki locally