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.
Employee Model:
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; }
}
EmployeeController:
namespace DataEntryMVCEFCore.Controllers
{
public class EmployeeController : Controller
{
private DataEntryContext _context;
public EmployeeController(DataEntryContext context)
{
_context = context;
}
// GET: /<controller>/
public IActionResult Index()
{
return View(_context.Employees.ToList());
}
// Populate Department values to DropDownList
private IEnumerable<SelectListItem> GetDepartments()
{
return _context.Departments
.Select(s => new SelectListItem
{
Value = s.DeptId.ToString(),
Text = s.DeptTitle
})
.ToList();
}
// Populate Building values to DropDownList
private IEnumerable<SelectListItem> GetBuildings()
{
return _context.Buildings
.Select(s => 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 => 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);
}
}
}
Employee Create View:
<form asp-controller="employee" asp-action="Create" method="post" class="form-horizontal" role="form">
<div class="form-horizontal">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="EmpFirstName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpFirstName" class="form-control" />
<span asp-validation-for="EmpFirstName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="EmpLastName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpLastName" class="form-control" />
<span asp-validation-for="EmpLastName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="DeptId" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*@Html.DropDownList("DeptList", ViewBag.DeptListName as SelectList, "Select Department")*@
<select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control">
<option>Please Select</option>
</select>
<span asp-validation-for="DeptId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="BldgId" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*@Html.DropDownList("BldgList", ViewBag.BldgListName as SelectList, "Select Building")*@
<select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control">
<option>Please Select</option>
</select>
<span asp-validation-for="BldgId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
Employee Edit View:
<form asp-controller="employee" asp-action="Edit" method="post" class="form-horizontal" role="form">
<div class="form-horizontal">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="EmpFirstName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpFirstName" class="form-control" />
<span asp-validation-for="EmpFirstName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="EmpLastName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpLastName" class="form-control" />
<span asp-validation-for="EmpLastName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="DeptId" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*@Html.DropDownList("DeptList", ViewBag.DeptListName as SelectList, "Select Department")*@
<select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control"></select>
<span asp-validation-for="DeptId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="BldgId" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*@Html.DropDownList("BldgList", ViewBag.BldgListName as SelectList, "Select Building")*@
<select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control"></select>
<span asp-validation-for="BldgId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>