public JsonResult GetCities(int countryID) { ContactsService contactService = new ContactsService(); ContactEditVM model = new ContactEditVM(); model.Cities = contactService.GetCitiesByCountry(countryID); return(Json(model.Cities.ToArray(), JsonRequestBehavior.AllowGet)); }
public ActionResult Edit() { UnitOfWork uf = new UnitOfWork(); ContactsService contactService = new ContactsService(uf); ContactEditVM model = new ContactEditVM(); TryUpdateModel(model); Contact c; if (model.ID != 0) { c = contactService.GetByID(model.ID); if (c == null) { return(this.RedirectToAction(co => co.List())); } } else { c = new Contact(); } if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) { var ext = Path.GetExtension(model.ImageUpload.FileName); if (String.IsNullOrEmpty(ext) || !ext.Equals(".jpg", StringComparison.OrdinalIgnoreCase)) { ModelState.AddModelError("", "Image format not accepted!"); } else { var uploadDir = "/Uploads/"; var imagePath = Path.Combine(Server.MapPath(uploadDir), model.ImageUpload.FileName); model.ImagePath = model.ImageUpload.FileName; model.ImageUpload.SaveAs(imagePath); } } if (!ModelState.IsValid) { model.Groups = contactService.GetSelectedGroups(c.Groups); model.Countries = contactService.GetAllCountries().OrderBy(country => country.Text); model.Cities = contactService.GetCitiesByCountry(model.CountryID); return(View(model)); } Mapper.Map(model, c); c.UserID = AuthenticationService.LoggedUser.ID; contactService.SetSelectedGroups(c, model.SelectedGroups); contactService.Save(c); return(this.RedirectToAction(co => co.List())); }
public ActionResult Edit(int?id) { ContactsService contactService = new ContactsService(); ContactEditVM model = new ContactEditVM(); Contact contact; if (id.HasValue) { contact = contactService.GetByID(id.Value); if (contact == null) { return(this.RedirectToAction(c => c.List())); } model.CountryID = contact.City.CountryID; } else { contact = new Contact(); contact.ImagePath = "default.png"; model.CountryID = int.Parse(contactService.GetAllCountries().FirstOrDefault().Value); } Mapper.Map(contact, model); if (model.ImagePath == null) { model.ImagePath = "default.png"; } model.Countries = contactService.GetAllCountries().OrderBy(c => c.Text); model.Cities = contactService.GetCitiesByCountry(model.CountryID); model.Groups = contactService.GetSelectedGroups(contact.Groups); return(View(model)); }