public virtual IActionResult AddressEdit(int addressId) { if (!_workContext.CurrentUser.IsRegistered()) { return(Challenge()); } var user = _workContext.CurrentUser; //find address (ensure that it belongs to the current user) var address = user.Addresses.FirstOrDefault(a => a.Id == addressId); if (address == null) { //address is not found return(RedirectToRoute("UserAddresses")); } var model = new UserAddressEditModel(); _addressModelFactory.PrepareAddressModel(model.Address, address: address, excludeProperties: false, addressSettings: _addressSettings, loadCountries: () => _countryService.GetAllCountries(_workContext.WorkingLanguage.Id)); return(View(model)); }
public virtual IActionResult AddressAdd(UserAddressEditModel model) { if (!_workContext.CurrentUser.IsRegistered()) { return(Challenge()); } var user = _workContext.CurrentUser; //custom address attributes var customAttributes = _addressAttributeParser.ParseCustomAddressAttributes(model.Form); var customAttributeWarnings = _addressAttributeParser.GetAttributeWarnings(customAttributes); foreach (var error in customAttributeWarnings) { ModelState.AddModelError("", error); } if (ModelState.IsValid) { var address = model.Address.ToEntity(); address.CustomAttributes = customAttributes; address.CreatedOnUtc = DateTime.UtcNow; //some validation if (address.CountryId == 0) { address.CountryId = null; } if (address.StateProvinceId == 0) { address.StateProvinceId = null; } //user.Addresses.Add(address); user.UserAddressMappings.Add(new UserAddressMapping { Address = address }); _userService.UpdateUser(user); return(RedirectToRoute("UserAddresses")); } //If we got this far, something failed, redisplay form _addressModelFactory.PrepareAddressModel(model.Address, address: null, excludeProperties: true, addressSettings: _addressSettings, loadCountries: () => _countryService.GetAllCountries(_workContext.WorkingLanguage.Id), overrideAttributesXml: customAttributes); return(View(model)); }
public virtual IActionResult AddressAdd() { if (!_workContext.CurrentUser.IsRegistered()) { return(Challenge()); } var model = new UserAddressEditModel(); _addressModelFactory.PrepareAddressModel(model.Address, address: null, excludeProperties: false, addressSettings: _addressSettings, loadCountries: () => _countryService.GetAllCountries(_workContext.WorkingLanguage.Id)); return(View(model)); }
public virtual IActionResult AddressEdit(UserAddressEditModel model, int addressId) { if (!_workContext.CurrentUser.IsRegistered()) { return(Challenge()); } var user = _workContext.CurrentUser; //find address (ensure that it belongs to the current user) var address = user.Addresses.FirstOrDefault(a => a.Id == addressId); if (address == null) { //address is not found return(RedirectToRoute("UserAddresses")); } //custom address attributes var customAttributes = _addressAttributeParser.ParseCustomAddressAttributes(model.Form); var customAttributeWarnings = _addressAttributeParser.GetAttributeWarnings(customAttributes); foreach (var error in customAttributeWarnings) { ModelState.AddModelError("", error); } if (ModelState.IsValid) { address = model.Address.ToEntity(address); address.CustomAttributes = customAttributes; _addressService.UpdateAddress(address); return(RedirectToRoute("UserAddresses")); } //If we got this far, something failed, redisplay form _addressModelFactory.PrepareAddressModel(model.Address, address: address, excludeProperties: true, addressSettings: _addressSettings, loadCountries: () => _countryService.GetAllCountries(_workContext.WorkingLanguage.Id), overrideAttributesXml: customAttributes); return(View(model)); }