public ActionResult EditDirectReports(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<EmployeeEditDirectReportsForm>(o);
// Fetch the employees into a temporary collection
// Logically, an employee cannot select "self" as a direct report
// 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 multi select list, configure the "selected" items
// Notice the use of the Select() method,
// which allows us to select/return/use only some properties from the source
var selectedValues = o.Employee1.Select(e => e.EmployeeId);
// Send the current direct reports, to display them on the form
form.DirectReports = o.Employee1.OrderBy(e => e.LastName).ThenBy(e => e.FirstName);
// For clarity, use the named parameter feature of C#
form.EmployeeList = new MultiSelectList
(items: employees,
dataValueField: "EmployeeId",
dataTextField: "FullName",
selectedValues: selectedValues);
return View(form);
}
}