protected AddressEditViewModel BuildViewModel(AddressPart part) { var viewModel = new AddressEditViewModel() { Address1 = part.Address1, Address2 = part.Address2, Address3 = part.Address3, AddressType = part.AddressType, PostalCode = part.PostalCode }; viewModel.Countries = _directoryService.GetCountries().Select(c => new SelectListItem() { Text = c.GetTitle(), Value = c.Id.ToString() }).ToList(); // TODO: Seriously AJAX this viewModel.Towns = _directoryService.GetTowns().Select(c => new SelectListItem() { Text = c.GetTitle(), Value = c.Id.ToString() }).ToList(); var town = _directoryService.GetTownForAddress(part); if (town != null) { viewModel.TownId = town.Id; viewModel.TownName = town.As <ITitleAspect>().Title; var country = _directoryService.GetCountryForTown(town); if (country != null) { viewModel.CountryId = country.Id; viewModel.CountryName = country.As <ITitleAspect>().Title; } } return(viewModel); }
private void FixUpdate(AddressEditViewModel vm) { // Country: front end sets the Id => we need to set the string var countryTP = _addressConfigurationService .GetCountry(vm.CountryId); vm.Country = _contentManager.GetItemMetadata(countryTP).DisplayText; // City: we may be settings either the Id or the string, but either way the // property we are setting is vm.City. We get the territory and set the Id var cityTP = GetTerritory(vm.City); if (cityTP != null) { vm.CityId = cityTP.Record.TerritoryInternalRecord.Id; vm.City = _contentManager.GetItemMetadata(cityTP).DisplayText; } else { vm.CityId = -1; } // Province: we may be settings either the Id or the string, but either way the // property we are setting is vm.Province. We get the territory and set the Id var provinceTP = GetTerritory(vm.Province); if (provinceTP != null) { vm.ProvinceId = provinceTP.Record.TerritoryInternalRecord.Id; vm.Province = _contentManager.GetItemMetadata(provinceTP).DisplayText; } else { vm.ProvinceId = -1; } }
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); 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 })); }
public ActionResult EditPost(int id) { 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 address = _nwazetCommunicationService.GetAddress(id, user); if (address == null) { return(HttpNotFound()); } var newAddress = new AddressEditViewModel(id); if (!TryUpdateModel(newAddress)) { _transactionManager.Cancel(); newAddress.Errors.Add(T("It was impossible to validate your address.").Text); return(View(newAddress)); } _nwazetCommunicationService.AddAddress(newAddress.AddressRecord, user); _notifier.Information(T("Address updated successfully.")); return(View(newAddress)); }
/// <summary> /// validation of the vm coming from a create/edit action /// </summary> /// <param name="vm"></param> /// <returns></returns> /// <remarks> /// It would be cleaner to do this in its own validation classes, /// but we need a bunch of IDependencies, so putting this code /// here is less of an hassle. /// </remarks> public bool Validate(AddressEditViewModel vm) { var validCountries = _addressConfigurationService .GetAllCountries(vm.AddressType); var countryTP = _addressConfigurationService .GetCountry(vm.CountryId); if (!SubValidation(validCountries, countryTP)) { return(false); } var validProvinces = _addressConfigurationService .GetAllProvinces(vm.AddressType, countryTP); if (validProvinces == null) { // error condition return(false); } var provinceTP = GetTerritory(vm.Province); if (validProvinces.Any()) { // there may not be any configured province, and that is ok, // but if any is configured, we check that the one selected is valid if (!SubValidation(validProvinces, provinceTP)) { return(false); } } var validCities = _addressConfigurationService .GetAllCities(vm.AddressType, // use province if it exists, otherwise country provinceTP == null ? countryTP : provinceTP); if (validCities == null) { // error condition return(false); } if (validCities.Any()) { // there may not be any configured city, and that is ok, // but if any is configured, we check that the one selected is valid var cityTP = GetTerritory(vm.City); if (!SubValidation(validCities, cityTP)) { return(false); } } return(true); }
private bool ValidateVM(AddressEditViewModel vm) { bool response = true; foreach (var valP in _validationProvider) { if (!valP.Validate(vm)) { response = false; } } return(response); }
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); }
/// <summary> /// </summary> /// <param name="viewModel"></param> /// <returns></returns> public async Task UpdateByViewModelAsync(AddressEditViewModel viewModel) { // Check if (viewModel == null) { throw new ArgumentNullException(nameof(viewModel)); } // Process var address = await _addressStore.FirstOrDefaultAsync(model => model.Id == viewModel.Id); _mapper.Map(viewModel, address); await _unitOfWork.SaveAllChangesAsync(auditUserId : _httpContextManager.CurrentUserId()); }
private Address AddressFromVM(AddressEditViewModel vm) { FixUpdate(vm); return(new Address { Honorific = vm.Honorific, FirstName = vm.FirstName, LastName = vm.LastName, Company = vm.Company, Address1 = vm.Address1, Address2 = vm.Address2, PostalCode = vm.PostalCode, // advanced address stuff // The string values here are the DisplayText properties of // configured territories, or "custom" text entered by the user. Country = vm.Country, City = vm.City, Province = vm.Province }); }
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; } } }
/// <summary> /// Konstruktor uruchamiany w celu edycji istniejącego wpisu /// </summary> public AddressEditWindow(Addresses entity, string connectionString) { try { InitializeComponent(); AddressEditViewModel viewModel = new AddressEditViewModel(entity, connectionString); //ViewModel z konstruktorem dla istniejącego wpisu this.DataContext = viewModel; //przypisanie logiki widoku do odpowiedniej klasy ViewModel Closing += viewModel.OnWindowClosing; //przypisanie metody wywołanej po zamknięciu okna Loaded += (s, e) => { if (DataContext is ICloseable) { (DataContext as ICloseable).RequestClose += (_, __) => this.Close(); } }; } catch (Exception ex) { MessageBox.Show(ex.Message); } }
protected AddressEditViewModel BuildCreateModel() { var viewModel = new AddressEditViewModel() { /* Address1 = part.Address1, * Address2 = part.Address2, * Address3 = part.Address3, * AddressType = part.AddressType, * PostalCode = part.PostalCode*/ }; viewModel.Countries = _directoryService.GetCountries().Select(c => new SelectListItem() { Text = c.GetTitle(), Value = c.Id.ToString() }).ToList(); // TODO: Seriously AJAX this viewModel.Towns = _directoryService.GetTowns().Select(c => new SelectListItem() { Text = c.GetTitle(), Value = c.Id.ToString() }).ToList(); return(viewModel); }
private AddressEditViewModel CreateVM(AddressRecordType addressRecordType, AddressEditViewModel vm) { return(AddressEditViewModel.CreateVM(_addressConfigurationService, addressRecordType, vm)); }
private AddressEditViewModel CreateVM() { return(AddressEditViewModel.CreateVM(_addressConfigurationService)); }
/// <summary> /// validation of the vm coming from a create/edit action /// </summary> /// <param name="vm"></param> /// <returns></returns> /// <remarks> /// It would be cleaner to do this in its own validation classes, /// but we need a bunch of IDependencies, so putting this code /// here is less of an hassle. /// </remarks> public bool Validate(AddressEditViewModel vm) { var validCountries = _addressConfigurationService .GetAllCountries(vm.AddressType); var countryTP = _addressConfigurationService .GetCountry(vm.CountryId); if (!SubValidation(validCountries, countryTP)) { return(false); } var provinceTP = GetTerritory(vm.Province) ?? _addressConfigurationService.SingleTerritory(vm.ProvinceId); if (provinceTP == null) { // maybe we did not find a territory because it's not configured, // but we had a free text input for the province var provinceName = vm.Province.Trim(); if (provinceName.Length < 2) { // at least two characters return(false); } } else { // check in the configuration parts if they are a valid province or not var adminTypePart = provinceTP.As <TerritoryAdministrativeTypePart>(); if (adminTypePart != null) { if (adminTypePart.AdministrativeType == TerritoryAdministrativeType.Province) { var territoryAddressTypePart = provinceTP.As <TerritoryAddressTypePart>(); if (territoryAddressTypePart != null) { if (vm.AddressType == AddressRecordType.ShippingAddress) { if (!territoryAddressTypePart.Shipping) { return(false); } } else { if (!territoryAddressTypePart.Billing) { return(false); } } } } } } var cityTP = GetTerritory(vm.City) ?? _addressConfigurationService.SingleTerritory(vm.CityId); // check in the configuration parts if they are a valid city or not if (cityTP != null) { var adminTypePart = cityTP.As <TerritoryAdministrativeTypePart>(); if (adminTypePart != null) { if (adminTypePart.AdministrativeType == TerritoryAdministrativeType.City) { var territoryAddressTypePart = cityTP.As <TerritoryAddressTypePart>(); if (territoryAddressTypePart != null) { if (vm.AddressType == AddressRecordType.ShippingAddress) { if (!territoryAddressTypePart.Shipping) { return(false); } } else { if (!territoryAddressTypePart.Billing) { return(false); } } } } } } // https://en.wikipedia.org/wiki/List_of_postal_codes // we had to make the change because we first checked // it was just a number // during use we noticed that the Netherlands have PS in the cap // therefore changed the control over the character if (string.IsNullOrWhiteSpace(vm.PostalCode)) { return(false); } else { foreach (char c in vm.PostalCode) { if (!char.IsLetterOrDigit(c) && !char.IsWhiteSpace(c)) { vm.Errors.Add(T("Postal or ZIP code may contain only characters or digits.").Text); return(false); } } } return(true); }
public AddressEditCTRL(Anonymous address) { this.InitializeComponent(); DataContext = new AddressEditViewModel(address); }