public ActionResult EditSupervisor(int? id)
{
// Attempt to fetch the matching object
var o = m.EmployeeGetByIdWIthOrgInfo(id.GetValueOrDefault());
if (o == null)
{
return HttpNotFound();
}
else
{
// Create a form, based on the fetched matching object
var form = AutoMapper.Mapper.Map<EmployeeEditSupervisorForm>(o);
// Fetch the employees into a temporary collection
// Logically, an employee cannot select "self" as the supervisor
// Therefore, we will remove the employee-being-edited from the collection
// We must use a concrete collection type, to be able to use the Remove() method
List<EmployeeBase> employees = m.EmployeeGetAll().ToList();
var thisEmployee = employees.SingleOrDefault(e => e.EmployeeId == o.EmployeeId);
employees.Remove(thisEmployee);
// For the select list, configure the "selected" item
// The easiest way is to read the value of ReportsTo
// It is possible that its value is null, so use HasValue
var selected = -1;
if (o.ReportsTo.HasValue)
{
selected = o.ReportsTo.GetValueOrDefault();
// Configure the "current supervisor" property, to display on the view
form.CurrentSupervisor = string.Format("{0}, {1}", o.Employee2.LastName, o.Employee2.FirstName);
}
// Create the new SelectList
// Use C# named parameters to eliminate ambiguity
form.EmployeeList = new SelectList
(items: employees,
dataValueField: "EmployeeId",
dataTextField: "FullName",
selectedValue: selected);
return View(form);
}
}