public ActionResult Edit(ContactCreateViewModel contactvmNew, HttpPostedFileBase file) { try { Contact _con = ContactsMapper.To_Contact_Create_ViewModel(contactvmNew); if (file != null) { string _FileName = Path.GetFileName(file.FileName); string _path = Path.Combine(Server.MapPath("~/UploadImg"), _FileName); _con.ImagePath = _FileName; file.SaveAs(_path); } else { _con.ImagePath = "images.png"; } _contactrepository.Update(_con); _contactrepository.Save(); List <PhoneNumber> _phonenumber = PhoneNumberMapper.To_PhoneNumber_Create_ViewModel(contactvmNew, _con.ContactId).ToList(); foreach (PhoneNumber phonItem in _phonenumber) { _phonenumberrepository.Update(phonItem); _phonenumberrepository.Save(); } return(RedirectToAction("Contacts")); } catch (DataException) { ModelState.AddModelError(string.Empty, "Unable to save changes. Try again."); } return(View(contactvmNew)); }
public ActionResult Create(ContactCreateViewModel contactcreatevm, HttpPostedFileBase file) { if (ModelState.IsValid) { Contact _contact = ContactsMapper.To_Contact_Create_ViewModel(contactcreatevm); try { if (file.ContentLength > 0) { string _FileName = Path.GetFileName(file.FileName); string _path = Path.Combine(Server.MapPath("~/UploadImg"), _FileName); _contact.ImagePath = _FileName; file.SaveAs(_path); } } catch { _contact.ImagePath = "images.png"; } finally { int val = 0; _contactrepository.Create(_contact, ref val); _contactrepository.Save(); List <PhoneNumber> _phonenumber = PhoneNumberMapper.To_PhoneNumber_Create_ViewModel(contactcreatevm, val).ToList(); foreach (PhoneNumber phonItem in _phonenumber) { _phonenumberrepository.Create(phonItem); _phonenumberrepository.Save(); } } return(RedirectToAction("Contacts")); } return(View("Create")); }