public ActionResult Edit(int?id) { ContactsService contactsService = new ContactsService(); ContactsEditVM model = new ContactsEditVM(); Contact contact; if (!id.HasValue) { contact = new Contact(); model.CountryID = int.Parse(contactsService.GetSelectedCountries().FirstOrDefault().Value); } else { contact = contactsService.GetByID(id.Value); if (contact == null) { return(this.RedirectToAction(c => c.List())); } model.CountryID = contact.City.CountryID; } Mapper.Map(contact, model); model.Countries = contactsService.GetSelectedCountries(); model.Cities = contactsService.GetCitiesByCountryID(model.CountryID); model.Groups = contactsService.GetSelectedGroups(contact.Groups); return(View(model)); }
public ActionResult Edit() { UnitOfWork unitOfWork = new UnitOfWork(); ContactsService contactsService = new ContactsService(unitOfWork); ContactsEditVM model = new ContactsEditVM(); TryUpdateModel(model); Contact contact; if (model.ID == 0) { contact = new Contact(); } else { contact = contactsService.GetByID(model.ID); if (contact == null) { return(this.RedirectToAction(c => c.List())); } } if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) { var imageExtension = Path.GetExtension(model.ImageUpload.FileName); if (String.IsNullOrEmpty(imageExtension) || !imageExtension.Equals(".jpg", StringComparison.OrdinalIgnoreCase)) { ModelState.AddModelError(string.Empty, "Wrong image format."); } else { string filePath = Server.MapPath("~/Uploads/"); model.ImagePath = model.ImageUpload.FileName; model.ImageUpload.SaveAs(filePath + model.ImagePath); } } if (!ModelState.IsValid) { model.Countries = contactsService.GetSelectedCountries(); model.Cities = contactsService.GetCitiesByCountryID(model.CountryID); model.Groups = contactsService.GetSelectedGroups(contact.Groups, model.SelectedGroups); return(View(model)); } Mapper.Map(model, contact); contact.UserID = AuthenticationManager.LoggedUser.ID; contactsService.UpdateContactGroups(contact, model.SelectedGroups); contactsService.Save(contact); return(this.RedirectToAction(c => c.List())); }