public async Task <IActionResult> Shipping(int storeScope, ShippingSettings settings, ShippingSettingsModel model) { var form = Request.Form; // Note, model state is invalid here due to ShippingOriginAddress validation. await MapperFactory.MapAsync(model, settings); await _storeDependingSettingHelper.UpdateSettingsAsync(settings, form, storeScope, propertyName => { // Skip to prevent the address from being recreated every time you save. if (propertyName.EqualsNoCase(nameof(settings.ShippingOriginAddressId))) { return(null); } return(propertyName); }); // Special case ShippingOriginAddressId\ShippingOriginAddress. if (storeScope == 0 || _storeDependingSettingHelper.IsOverrideChecked(settings, "ShippingOriginAddress", form)) { var addressId = await Services.Settings.SettingExistsAsync(settings, x => x.ShippingOriginAddressId, storeScope) ? settings.ShippingOriginAddressId : 0; var originAddress = await _db.Addresses.FindByIdAsync(addressId) ?? new Address { CreatedOnUtc = DateTime.UtcNow }; // Update ID manually (in case we're in multi-store configuration mode it'll be set to the shared one). model.ShippingOriginAddress.Id = originAddress.Id == 0 ? 0 : addressId; await MapperFactory.MapAsync(model.ShippingOriginAddress, originAddress); if (originAddress.Id == 0) { _db.Addresses.Add(originAddress); await _db.SaveChangesAsync(); } settings.ShippingOriginAddressId = originAddress.Id; await Services.Settings.ApplySettingAsync(settings, x => x.ShippingOriginAddressId, storeScope); } else { _db.Addresses.Remove(settings.ShippingOriginAddressId); await Services.Settings.RemoveSettingAsync(settings, x => x.ShippingOriginAddressId, storeScope); } await _db.SaveChangesAsync(); return(NotifyAndRedirect("Shipping")); }