public ActionResult Edit( [Bind( Include = "CompanyName,CompanyId,ContactId,Title,Name,Surname,Email,Phone,Mobile,Position,ParentContactId,Notes" )] Contact contact ) { contact.Companies = GetCompanies(); contact.ContactNames = GetContacts(); if (ModelState.IsValid) { using (CompanyContactsContext db = new CompanyContactsContext()) { db.Entry( contact ).State = EntityState.Modified; db.SaveChanges(); } return RedirectToAction( "Index" ); } return View( contact ); }
public ActionResult Edit( [Bind( Include = "CompanyName,CompanyRef,NatureOfBusiness,KeyContact,OnSite,EmergencyContact" )] CompanyContactDetailsViewModel companyView ) { Company company = new Company(); company.CompanyName = companyView.CompanyName; company.CompanyRef = companyView.CompanyRef; company.NatureOfBusiness = companyView.NatureOfBusiness; companyView.ContactNames = GetContacts(); if (ModelState.IsValid) { using (CompanyContactsContext db = new CompanyContactsContext()) { // We may have just made a contact the key or emergency contact. If so, ensure that only one // person is the specified contact for this company. foreach ( Contact contact in db.Contacts) { if (contact.CompanyName.Equals( company.CompanyName )) { contact.KeyContact = false; contact.OnSite = false; if (contact.ContactId == companyView.KeyContact) { contact.KeyContact = true; contact.OnSite = companyView.OnSite; } contact.EmergencyContact = false; if (contact.ContactId == companyView.EmergencyContact) { contact.EmergencyContact = true; } db.Entry( contact ).State = EntityState.Modified; } } db.Entry( company ).State = EntityState.Modified; db.SaveChanges(); } return RedirectToAction( "Index" ); } return View( companyView ); }
public ActionResult DeleteConfirmed( int id ) { using (CompanyContactsContext db = new CompanyContactsContext()) { // Need to null out any references to this contact as being a manger. Note that we do *not* want to implement a cascade delete ! foreach (Contact staff in db.Contacts) { if (staff.ParentContactId == id) { staff.ParentContactId = null; db.Entry( staff).State = EntityState.Modified; } } db.SaveChanges(); Contact contact = db.Contacts.Find(id); db.Contacts.Remove( contact ); db.SaveChanges(); } return RedirectToAction( "Index" ); }