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; } } } var cityId = address.CityId; if (cityId <= 0 && !string.IsNullOrWhiteSpace(address.City)) { var tp = _addressConfigurationService.GetCity(address.City); if (tp != null) { cityId = tp.Record.TerritoryInternalRecord.Id; } } var provinceId = address.ProvinceId; if (provinceId <= 0 && !string.IsNullOrWhiteSpace(address.Province)) { var tp = _addressConfigurationService.GetProvince(address.Province); if (tp != null) { provinceId = tp.Record.TerritoryInternalRecord.Id; } } return(new AddressEditViewModel(address) { Countries = _addressConfigurationService .CountryOptions(address.AddressType, countryId), ShippingCountries = _addressConfigurationService.CountryOptions(AddressRecordType.ShippingAddress), BillingCountries = _addressConfigurationService.CountryOptions(AddressRecordType.BillingAddress), CountryId = countryId, CityId = cityId, ProvinceId = provinceId }); }
private bool ValidateVM(AddressEditViewModel vm) { int id = -1; if (vm.CityId > 0) { if (int.TryParse(vm.City, out id)) { // the form sent the city's id instead of its name vm.City = _addressConfigurationService .GetCity(vm.CityId) ?.As <TitlePart>() ?.Title ?? string.Empty; } } if (vm.ProvinceId > 0) { if (int.TryParse(vm.Province, out id)) { // the form sent the city's id instead of its name vm.Province = _addressConfigurationService .GetProvince(vm.ProvinceId) ?.As <TitlePart>() ?.Title ?? string.Empty; } } if (vm.CountryId > 0) { if (int.TryParse(vm.Country, out id)) { // the form sent the city's id instead of its name vm.Country = _addressConfigurationService .GetCountry(vm.CountryId) ?.As <TitlePart>() ?.Title ?? string.Empty; } } bool response = true; foreach (var valP in _validationProviders) { if (!valP.Validate(vm)) { response = false; } } return(response); }
private void ValidateVM(AddressEditViewModel vm) { int id = -1; if (vm.CityId > 0) { if (int.TryParse(vm.City, out id)) { // the form sent the city's id instead of its name vm.City = _addressConfigurationService .GetCity(vm.CityId) ?.As <TitlePart>() ?.Title ?? string.Empty; } } if (vm.ProvinceId > 0) { if (int.TryParse(vm.Province, out id)) { // the form sent the city's id instead of its name vm.Province = _addressConfigurationService .GetProvince(vm.ProvinceId) ?.As <TitlePart>() ?.Title ?? string.Empty; } } if (vm.CountryId > 0) { if (int.TryParse(vm.Country, out id)) { // the form sent the city's id instead of its name vm.Country = _addressConfigurationService .GetCountry(vm.CountryId) ?.As <TitlePart>() ?.Title ?? string.Empty; } } }