public ActionResult Edit(int?id) { PhonesService phonesService = new PhonesService(); PhonesEditVM model = new PhonesEditVM(); TryUpdateModel(model); Phone phone; if (!id.HasValue) { phone = new Phone(); } else { phone = phonesService.GetByID(id.Value); if (phone == null) { if (phonesService.GetContact(model.ContactID) == null) { return(this.RedirectToAction <ContactsController>(c => c.List())); } return(this.RedirectToAction(c => c.List(), new { ContactID = model.ContactID })); } model.ContactID = phone.ContactID; } Mapper.Map(phone, model); return(View(model)); }
public ActionResult List() { PhonesService phonesService = new PhonesService(); PhonesListVM model = new PhonesListVM(); TryUpdateModel(model); if (!model.ContactID.HasValue || phonesService.GetContact(model.ContactID.Value) == null) { return(this.RedirectToAction <ContactsController>(c => c.List())); } model.Phones = phonesService.GetAll().Where(p => p.ContactID == model.ContactID.Value).ToList(); model.Contact = phonesService.GetContact(model.ContactID.Value); return(View(model)); }
public ActionResult Edit() { PhonesService phonesService = new PhonesService(); PhonesEditVM model = new PhonesEditVM(); TryUpdateModel(model); if (phonesService.GetContact(model.ContactID) == null) { return(this.RedirectToAction <ContactsController>(c => c.List())); } Phone phone; if (model.ID == 0) { phone = new Phone(); } else { phone = phonesService.GetByID(model.ID); if (phone == null) { return(this.RedirectToAction(c => c.List(), new { ContactID = model.ContactID })); } } if (!ModelState.IsValid) { return(View(model)); } Mapper.Map(model, phone); phonesService.Save(phone); return(this.RedirectToAction(c => c.List(), new { ContactID = model.ContactID })); }