public ActionResult Index() { // Get all contacts List<Contact> contacts = _contactUtilities.GetAllContacts(); //Build flat view model. List<ContactViewModel> flatContacts = new List<ContactViewModel>(); foreach (var contact in contacts) { ContactViewModel flatContact = new ContactViewModel { ContactId = contact.ContactId, DateOfBirth = contact.DateOfBirth, Email = contact.Email, FirstName = contact.FirstName, LastName = contact.LastName, JoinDate = contact.JoinDate, Organisation = contact.Organisation, Phone = contact.Phone, Title = contact.Title }; if (contact.Manager == null) { } else { flatContact.ManagerFirstName = contact.Manager.FirstName; flatContact.ManagerLastName = contact.Manager.LastName; } flatContacts.Add(flatContact); } return View(flatContacts); }
// GET: Inspect public ActionResult Inspect(string contactId) { if (string.IsNullOrEmpty(contactId)) { return RedirectToAction("Index", "Home"); } Contact contact = _contactUtilities.GetContactById(Convert.ToInt16(contactId)); // Build flat contact ContactViewModel flatContact = new ContactViewModel { ContactId = contact.ContactId, DateOfBirth = contact.DateOfBirth, Email = contact.Email, FirstName = contact.FirstName, LastName = contact.LastName, JoinDate = contact.JoinDate, Organisation = contact.Organisation, Phone = contact.Phone, Title = contact.Title }; if (contact.Manager != null) { flatContact.ManagerFirstName = contact.Manager.FirstName; flatContact.ManagerLastName = contact.Manager.LastName; } if (contact.Picture != null) { flatContact.Picture = contact.Picture; } return View(flatContact); }
/// <summary> /// Fetch the indicated contact, build flat ViewModel for the Edit screen. /// </summary> /// <param name="contactId">The contact to build the view model for.</param> /// <returns>The view model for the edit screen.</returns> public ContactViewModel GetViewModelForEdit(int contactId) { // Get the indicated contact Contact contact = _contactUtilities.GetContactById(contactId); // Transfer into our flat ViewModel class ContactViewModel flatContact = new ContactViewModel { ContactId = contact.ContactId, DateOfBirth = contact.DateOfBirth, Email = contact.Email, FirstName = contact.FirstName, LastName = contact.LastName, JoinDate = contact.JoinDate, Organisation = contact.Organisation, Phone = contact.Phone, Title = contact.Title }; if (contact.Manager != null) { flatContact.ManagerId = contact.Manager.ContactId; flatContact.ManagerFirstName = contact.Manager.FirstName; flatContact.ManagerLastName = contact.Manager.LastName; } if (contact.Picture != null) { flatContact.Picture = contact.Picture; } // Get all possible managers for this contact (all contacts excluding themself). flatContact.PossibleManagers = _contactUtilities.GetAllContactsExceptOne(contactId); return flatContact; }