public static AddressEditViewModel CreateVM( IAddressConfigurationService _addressConfigurationService, AddressRecordType addressRecordType, AddressEditViewModel viewModel) { if (viewModel == null) { viewModel = CreateVM(_addressConfigurationService); } else { viewModel.Countries = _addressConfigurationService .CountryOptions(); if (addressRecordType == AddressRecordType.ShippingAddress) { viewModel.ShippingCountries = _addressConfigurationService .CountryOptions(AddressRecordType.ShippingAddress, viewModel.CountryId); } if (addressRecordType == AddressRecordType.BillingAddress) { viewModel.BillingCountries = _addressConfigurationService .CountryOptions(AddressRecordType.BillingAddress, viewModel.CountryId); } if (viewModel.ProvinceId <= 0 && !string.IsNullOrWhiteSpace(viewModel.Province)) { viewModel.ProvinceId = -1; } if (viewModel.CityId <= 0 && !string.IsNullOrWhiteSpace(viewModel.City)) { viewModel.CityId = -1; } } viewModel.AddressType = addressRecordType; return(viewModel); }
public static AddressEditViewModel CreateVM( IAddressConfigurationService _addressConfigurationService) { return(new AddressEditViewModel() { Countries = _addressConfigurationService .CountryOptions(), ShippingCountries = _addressConfigurationService .CountryOptions(AddressRecordType.ShippingAddress), BillingCountries = _addressConfigurationService .CountryOptions(AddressRecordType.BillingAddress) }); }
private AddressEditViewModel CreateVM(AddressRecord address) { // defaults to "no country selected" for a new or "legacy" AddressRecord var countryId = address.CountryId; if (countryId == 0 && !string.IsNullOrWhiteSpace(address.Country)) { // from address.Country, find the value that should be used // address.Country is of type string. It could represent the // name of the country (legacy) or the Id of the country territory. // Try to parse it. if (!int.TryParse(address.Country, out countryId)) { // parsing failed, so the string may be a territory's name var tp = _addressConfigurationService.GetCountry(address.Country); if (tp != null) { countryId = tp.Record.TerritoryInternalRecord.Id; } } } return(new AddressEditViewModel(address) { Countries = _addressConfigurationService .CountryOptions(address.AddressType, countryId), CountryId = countryId }); }
public ActionResult CreatePost() { var user = _workContextAccessor.GetContext().CurrentUser; if (user == null) { // we should never be here, because the AuthorizeAttribute should // take care of anonymous users. return(new HttpUnauthorizedResult(T("Sign In to manage your saved addresses.").Text)); } var newAddress = new AddressEditViewModel(); if (!TryUpdateModel(newAddress) || !ValidateVM(newAddress)) { _transactionManager.Cancel(); // added the assignment of the lists because in case of error in the validation the properties are not populated newAddress.ShippingCountries = _addressConfigurationService.CountryOptions(AddressRecordType.ShippingAddress); newAddress.BillingCountries = _addressConfigurationService.CountryOptions(AddressRecordType.BillingAddress); FixUpdate(newAddress); newAddress.Errors.Add(T("It was impossible to validate your address.").Text); return(View(newAddress)); } // Convert the values of Country, City, and Province to strings and ids for // the AddressRecord. FixUpdate(newAddress); // save record _nwazetCommunicationService.AddAddress(newAddress.AddressRecord, user); _notifier.Information(T("Address created successfully.")); return(RedirectToAction("Edit", new { id = newAddress.AddressRecord.Id })); }