/// <summary>
        /// Convert an <see cref="AddressViewModel"/> to an <see cref="Address"/>.
        /// </summary>
        /// <param name="model">
        /// The model.
        /// </param>
        /// <param name="db">
        /// The db.
        /// </param>
        /// <returns>
        /// The <see cref="Address"/>.
        /// </returns>
        public static Address ToEntity(this AddressViewModel model, ProviderPortalEntities db)
        {
            // Fix the postcode format
            model.Postcode = model.Postcode.ToUpper();
            if (model.Postcode.IndexOf(" ") == -1 && model.Postcode.Length > 3)
            {
                model.Postcode = model.Postcode.Substring(0, model.Postcode.Length - 3) + " " + model.Postcode.Substring(model.Postcode.Length - 3, 3);
            }

            Address address = model.AddressId == 0 ? new Address() : db.Addresses.Find(model.AddressId);

            address.AddressLine1 = model.AddressLine1;
            address.AddressLine2 = model.AddressLine2;
            address.County       = model.County;
            address.Town         = model.Town;
            address.Postcode     = model.Postcode.ToUpper();
            if (!model.HideRegion)
            {
                address.ProviderRegionId = model.RegionId;
            }

            if (model.AddressBaseId != null)
            {
                AddressBase addressBase = db.AddressBases.Find(model.AddressBaseId);
                if (addressBase != null && String.Equals(addressBase.Postcode, model.Postcode, StringComparison.CurrentCultureIgnoreCase))
                {
                    address.Latitude  = addressBase.Latitude == null ? (Double?)null : Convert.ToDouble(addressBase.Latitude.Value);
                    address.Longitude = addressBase.Longitude == null ? (Double?)null : Convert.ToDouble(addressBase.Longitude.Value);
                }
            }

            if (address.Latitude == null)
            {
                GeoLocation geo = db.GeoLocations.Find(address.Postcode);
                if (geo != null)
                {
                    address.Latitude  = geo.Lat;
                    address.Longitude = geo.Lng;
                }
            }

            return(address);
        }
 public ProviderViewModel()
 {
     Address = new AddressViewModel();
     Venues  = new List <ListVenueModel>();
 }
 /// <summary>
 /// Populate an <see cref="AddressViewModel"/>.
 /// </summary>
 /// <param name="model">
 /// The model.
 /// </param>
 /// <param name="db">
 /// The db.
 /// </param>
 /// <returns>
 /// The <see cref="AddressViewModel"/>.
 /// </returns>
 public static AddressViewModel Populate(this AddressViewModel model, ProviderPortalEntities db)
 {
     model.Regions = GetRegions(db, model);
     return(model);
 }
 public static String GetMultipleLineHTMLAddress(this AddressViewModel address)
 {
     return(GetFullAddress(address, "<br />"));
 }
 public static String GetSingleLineHTMLAddress(this AddressViewModel address)
 {
     return(GetFullAddress(address, ", "));
 }
        /// <summary>
        /// Get the regions select list.
        /// </summary>
        /// <param name="db">
        /// The db.
        /// </param>
        /// <param name="model"></param>
        /// <returns>
        /// The <see />.
        /// </returns>
        private static IEnumerable <SelectListItem> GetRegions(ProviderPortalEntities db, AddressViewModel model)
        {
            var items = (List <SelectListItem>)CacheManagement.CacheHandler.Get(CacheKey);

            if (items == null)
            {
                items = new SelectList(db.ProviderRegions.OrderBy(x => x.RegionName), "ProviderRegionId", "RegionName", model.RegionId).ToList();
                items.Insert(
                    0,
                    new SelectListItem
                {
                    Value = string.Empty,
                    Text  =
                        AppGlobal.Language.GetText("Address_Region_SelectRegion", "Select a region")
                });
                CacheManagement.CacheHandler.Add(CacheKey, items);
            }
            return(items);
        }