title: ASP.NET MVC Retrieve Database value to DropDownList on Edit
body: <p>I'm having trouble retrieving a list of my Departments and Buildings when I attempt to "Edit" an Employee. I'm able to retrieve the Employees First and Last Name but my DropDownList for Departments and Buildings on the Edit View of Employees are empty. Please see my Models, Controllers and Views below.</p>
  
  <p><strong>Employee Model:</strong></p>
  
  <pre><code>public class Employee
  {
      [Key]
      public int EmpId { get; set; }
      [Required]
      public string EmpFirstName { get; set; }
      [Required]
      public string EmpLastName { get; set; }
  
      public int DeptId { get; set; }
      public Department Department { get; set; }
  
      public int BldgId { get; set; }
      public Building Building { get; set; }
  }
  </code></pre>
  
  <p><strong>EmployeeController:</strong></p>
  
  <pre><code>namespace DataEntryMVCEFCore.Controllers
  {
      public class EmployeeController : Controller
      {
          private DataEntryContext _context;
  
          public EmployeeController(DataEntryContext context)
          {
              _context = context;
          }
  
          // GET: /&lt;controller&gt;/
          public IActionResult Index()
          {
              return View(_context.Employees.ToList());
          }
  
          // Populate Department values to DropDownList
          private IEnumerable&lt;SelectListItem&gt; GetDepartments()
          {
              return _context.Departments
                            .Select(s =&gt; new SelectListItem
                            {
                                Value = s.DeptId.ToString(),
                                Text = s.DeptTitle
                            })
                            .ToList();
          }
  
          // Populate Building values to DropDownList
          private IEnumerable&lt;SelectListItem&gt; GetBuildings()
          {
              return _context.Buildings
                            .Select(s =&gt; new SelectListItem
                            {
                                Value = s.BldgId.ToString(),
                                Text = s.BldgName
                            })
                            .ToList();
          }
  
          public IActionResult Create()
          {
              // Load values to DropDownLists for Departments and Buildings for Create Method
              ViewBag.DeptListName = GetDepartments();
              ViewBag.BldgListName = GetBuildings();
  
              return View();
          }
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public IActionResult Create(Employee employee)
          {
              if (ModelState.IsValid)
              {
                  _context.Employees.Add(employee);
                  _context.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              return View(employee);
          }
  
          public IActionResult Edit(int id)
          {
              var Employee = _context.Employees
                  .Where(e =&gt; e.EmpId == id)
                  .Single();
              return View(Employee);
          }
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public IActionResult Edit(Employee employee)
          {
              if (ModelState.IsValid)
              {
                  _context.Employees.Update(employee);
                  _context.SaveChanges();
  
                  return RedirectToAction("Index");
              }
  
              return View(employee);
          }
      }
  }
  </code></pre>
  
  <p><strong>Employee Create View:</strong></p>
  
  <pre><code>&lt;form asp-controller="employee" asp-action="Create" method="post" class="form-horizontal" role="form"&gt;
  &lt;div class="form-horizontal"&gt;
      &lt;div asp-validation-summary="All" class="text-danger"&gt;&lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label asp-for="EmpFirstName" class="col-md-2 control-label"&gt;&lt;/label&gt;
          &lt;div class="col-md-10"&gt;
              &lt;input asp-for="EmpFirstName" class="form-control" /&gt;
              &lt;span asp-validation-for="EmpFirstName" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label asp-for="EmpLastName" class="col-md-2 control-label"&gt;&lt;/label&gt;
          &lt;div class="col-md-10"&gt;
              &lt;input asp-for="EmpLastName" class="form-control" /&gt;
              &lt;span asp-validation-for="EmpLastName" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label asp-for="DeptId" class="col-md-2 control-label"&gt;&lt;/label&gt;
          &lt;div class="col-md-10"&gt;      
              @*@Html.DropDownList("DeptList", ViewBag.DeptListName as SelectList, "Select Department")*@  
              &lt;select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control"&gt;
                  &lt;option&gt;Please Select&lt;/option&gt;
              &lt;/select&gt;
              &lt;span asp-validation-for="DeptId" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;label asp-for="BldgId" class="col-md-2 control-label"&gt;&lt;/label&gt;
          &lt;div class="col-md-10"&gt;
              @*@Html.DropDownList("BldgList", ViewBag.BldgListName as SelectList, "Select Building")*@
              &lt;select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control"&gt;
                  &lt;option&gt;Please Select&lt;/option&gt;
              &lt;/select&gt;
              &lt;span asp-validation-for="BldgId" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-offset-2 col-md-10"&gt;
              &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>Employee Edit View:</strong></p>
  
  <pre><code>&lt;form asp-controller="employee" asp-action="Edit" method="post" class="form-horizontal" role="form"&gt;
      &lt;div class="form-horizontal"&gt;
          &lt;div asp-validation-summary="All" class="text-danger"&gt;&lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="EmpFirstName" class="col-md-2 control-label"&gt;&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  &lt;input asp-for="EmpFirstName" class="form-control" /&gt;
                  &lt;span asp-validation-for="EmpFirstName" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="EmpLastName" class="col-md-2 control-label"&gt;&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  &lt;input asp-for="EmpLastName" class="form-control" /&gt;
                  &lt;span asp-validation-for="EmpLastName" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="DeptId" class="col-md-2 control-label"&gt;&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  @*@Html.DropDownList("DeptList", ViewBag.DeptListName as SelectList, "Select Department")*@
                  &lt;select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control"&gt;&lt;/select&gt;
                  &lt;span asp-validation-for="DeptId" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;label asp-for="BldgId" class="col-md-2 control-label"&gt;&lt;/label&gt;
              &lt;div class="col-md-10"&gt;
                  @*@Html.DropDownList("BldgList", ViewBag.BldgListName as SelectList, "Select Building")*@
                  &lt;select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control"&gt;&lt;/select&gt;
                  &lt;span asp-validation-for="BldgId" class="text-danger"&gt;&lt;/span&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>