public PartialViewResult AddContactPerson(AddressModel model) { if (!model.LanguageId.HasValue) { model.LanguageId = _workContext.WorkingLanguage.Id; } var customer = _workContext.CurrentCustomer; if (model.Id != 0) { //find address (ensure that it belongs to the current customer) var address = customer.Addresses.Where(a => a.Id == model.Id).FirstOrDefault(); if (ModelState.IsValid) { address = model.ToEntity(address); _addressService.UpdateAddress(address); } return(AddContactPersonPostback(model.LanguageId.Value)); } if (ModelState.IsValid) { var address = model.ToEntity(); address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) { address.CountryId = null; } if (address.StateProvinceId == 0) { address.StateProvinceId = null; } customer.Addresses.Add(address); _customerService.UpdateCustomer(customer); return(AddContactPersonPostback(model.LanguageId.Value)); } return(AddContactPersonPostback(model.LanguageId.Value)); }
public ActionResult SaveAddress(AddressModel model) { if (ModelState.IsValid) { var address = _addressRepository.GetById(model.Id); //always set IsNew to false when saving address.IsNew = false; address = model.ToEntity(address); _addressRepository.UpdateAndCommit(address); return(new NullJsonResult()); } else { return(Json(new { Errors = ModelState.Errors().ToHtmlString() })); } }