static IReadOnlyDictionary <string, AreaItems> LoadAreas() { GeoCoderOptions geoCoderOptions = Shashlik.Kernel.GlobalKernelServiceProvider.KernelServiceProvider .GetService <IOptions <GeoCoderOptions> >().Value; var all = geoCoderOptions.Areas; Dictionary <string, AreaItems> result = new Dictionary <string, AreaItems>(); var provinceList = all[RootCode]; foreach (var provinceItem in provinceList) { AreaItems province = new AreaItems { Value = provinceItem.Key, Label = provinceItem.Value, Children = new Dictionary <string, AreaItems>() }; if (!all.ContainsKey(provinceItem.Key)) { continue; } var cities = all[provinceItem.Key]; foreach (var cityItem in cities) { AreaItems city = new AreaItems { Value = cityItem.Key, Label = cityItem.Value, Children = new Dictionary <string, AreaItems>() }; if (!all.ContainsKey(cityItem.Key)) { continue; } var areas = all[cityItem.Key]; foreach (var areaItem in areas) { AreaItems area = new AreaItems { Value = areaItem.Key, Label = areaItem.Value, }; city.Children.Add(areaItem.Key, area); } province.Children.Add(cityItem.Key, city); } result.Add(provinceItem.Key, province); } return(result); }
public bool IsValid(AreaModel areaModel, AreaLevel level, out string errMsg) { errMsg = ""; AreaItems p = null; if (level >= AreaLevel.省) { if (!Areas.ContainsKey(areaModel.ProvinceCode)) { errMsg = "请选择省"; return(false); } p = Areas[areaModel.ProvinceCode]; } AreaItems c = null; if (level >= AreaLevel.市) { if (areaModel.CityCode.IsNullOrWhiteSpace() || !p.Children.ContainsKey(areaModel.CityCode)) { errMsg = "请选择市"; return(false); } c = p.Children[areaModel.CityCode]; } if (level >= AreaLevel.区) { if (areaModel.AreaCode.IsNullOrWhiteSpace() || !c.Children.ContainsKey(areaModel.AreaCode)) { errMsg = "请选择区"; return(false); } } return(true); }