public IContent CreateAddress(ViewModels.AddressEditViewModel viewModel, IUpdateModel updater, string prefix)
        {
            var address = _services.ContentManager.New(AddressesContentType);
            var part    = address.As <AddressPart>();

            if (part != null)
            {
                if (PopulateAddressFromViewModel(part, viewModel, updater, prefix))
                {
                    // Publish
                    _services.ContentManager.Create(address);
                    if (!LinkAddressToTown(part, viewModel.TownId.Value))
                    {
                        updater.AddModelError(prefix + ".TownName", T("Error saving town. Enter a valid town name or select from the list."));
                        _services.ContentManager.Remove(address);
                    }
                    return(address);
                }
            }
            return(null);
        }
        public bool PopulateAddressFromViewModel(AddressPart part, ViewModels.AddressEditViewModel viewModel, IUpdateModel updater, string prefix)
        {
            var ok = true;

            // Copy normal fields from view model
            if (String.IsNullOrWhiteSpace(viewModel.PostalCode))
            {
                updater.AddModelError(prefix + ".PostalCode", T("Please enter a post code."));
                ok = false;
            }
            else
            {
                // Strip any spaces
                part.PostalCode = viewModel.PostalCode = viewModel.PostalCode.Replace(" ", "");
            }

            part.AddressType = viewModel.AddressType;
            if (String.IsNullOrWhiteSpace(viewModel.Address1))
            {
                updater.AddModelError(prefix + ".Address1", T("Please enter at least the first line of the street address."));
                ok = false;
            }
            part.Address1 = viewModel.Address1;
            part.Address2 = viewModel.Address2;
            part.Address3 = viewModel.Address3;

            // Attempt to create inputted country name
            if (!String.IsNullOrWhiteSpace(viewModel.CountryName))
            {
                try
                {
                    var country = GetOrCreateCountryByName(viewModel.CountryName);
                    if (country != null)
                    {
                        viewModel.CountryId = country.Id;
                    }
                }
                catch (Exception e)
                {
                    Logger.Error(e, "Couldn't create country");
                }
            }
            // Attempt to create inputted region name
            if (!viewModel.CountryId.HasValue)
            {
                updater.AddModelError(prefix + ".CountryName", T("Error saving country. Enter a valid country name or select from the list."));
                ok = false;
            }
            if (viewModel.CountryId.HasValue && !String.IsNullOrWhiteSpace(viewModel.TownName))
            {
                var region = GetOrCreateTownByName(viewModel.CountryId.Value, viewModel.TownName);
                if (region != null)
                {
                    viewModel.TownId = region.Id;
                }
            }
            if (!viewModel.TownId.HasValue)
            {
                updater.AddModelError(prefix + ".TownName", T("Enter a valid town name or select from the list."));
                ok = false;
            }
            return(ok);
        }