前台代码
@{ IList<Provinces> allProvinces = ViewBag.AllProvinces; IList<Districts> allDistricts = ViewBag.AllDistricts; IList<Cities> allCities = ViewBag.AllCities; } <html> <head> <script src="/js/jquery-1.11.3.js" charset="utf-8"></script> <script type="text/javascript"> var _allProvinces = @(new MvcHtmlString(JsonHelper.SerializeToJson(allProvinces.ToDictionary(k=>k.ProvinceID,v=>v.Province)))); var _allDistricts = @(new MvcHtmlString(JsonHelper.SerializeToJson(allDistricts.GroupBy(a => a.ParentID).ToDictionary(k => k.Key, v => v.ToDictionary(k1 => k1.DistrictID, v1 => v1.District))))); var _allCities = @(new MvcHtmlString(JsonHelper.SerializeToJson(allCities.GroupBy(a=>a.ParentID).ToDictionary(k=>k.Key, v=> v.ToDictionary(k1=>k1.CityID, v1=>v1.City))))); $(function () { var cityID =@(Model.AddressID); var provincesID = ""; var districtsID = ""; for(var key in _allCities) { for(var temp in _allCities[key]) { if(temp == cityID){ districtsID = key; } } } for(var key in _allDistricts) { for(var temp in _allDistricts[key]) { if(temp == districtsID){ provincesID = key; } } } for(var key in _allProvinces) { $("#sltProvinces").append("<option value=‘"+key+"‘>"+_allProvinces[key]+"</option>"); } if(provincesID!=""){ $("#sltProvinces").find("option[value=‘"+provincesID+"‘]").attr("selected",true); } var parentID = $("#sltProvinces").val(); fullDistricts(parentID); if(districtsID!=""){ $("#sltDistricts").find("option[value=‘"+districtsID+"‘]").attr("selected",true); } parentID = $("#sltDistricts").val(); fullCities(parentID); if(cityID!=""){ $("#sltCities").find("option[value=‘"+cityID+"‘]").attr("selected",true); } $("#sltProvinces").change(function(){ var provinceID = $(this).val(); fullDistricts(provinceID); var districtsID = $("#sltDistricts").val(); fullCities(districtsID); }); $("#sltDistricts").change(function(){ var districtsID = $(this).val(); fullCities(districtsID); }); @*填充市*@ function fullDistricts(parentID){ var districts; for(var key in _allDistricts) { if(key == parentID) { districts = _allDistricts[key]; } } $("#sltDistricts").empty(); for(var key in districts) { $("#sltDistricts").append("<option value=‘"+key+"‘>"+districts[key]+"</option>"); } } @*填充县*@ function fullCities(parentID){ var cities; for(var key in _allCities) { if(key == parentID) { cities = _allCities[key]; } } $("#sltCities").empty(); for(var key in cities) { $("#sltCities").append("<option value=‘"+key+"‘>"+cities[key]+"</option>"); } } </script> </head> <body> <select id="sltProvinces"></select> <select id="sltDistricts"></select> <select id="sltCities"></select> </body> </html>
mvc中
就是获取数据库中的数据
#region 省市县 IList<Provinces> allProvinces; IList<Districts> allDistricts; IList<Cities> allCities; Dictionary<string, object> param3 = new Dictionary<string, object>(); string dataUrl = RouteManager.GetApiRoute("Index", "GetAllProvinces", param3); allProvinces = JsonConvert.DeserializeObject<IList<Provinces>>(ShopList.PostShop(dataUrl)); dataUrl = RouteManager.GetApiRoute("Index", "GetAllDistricts", param3); allDistricts = JsonConvert.DeserializeObject<IList<Districts>>(ShopList.PostShop(dataUrl)); dataUrl = RouteManager.GetApiRoute("Index", "GetAllCities", param3); allCities = JsonConvert.DeserializeObject<IList<Cities>>(ShopList.PostShop(dataUrl)); ViewBag.AllProvinces = allProvinces; ViewBag.AllCities = allCities; ViewBag.AllDistricts = allDistricts; #endregion
api中
#region 行政区 省市县 /// <summary> /// 获取所有省 /// </summary> /// <returns></returns> IList<Provinces> IIndexDAO.GetAllProvinces() { IList<Provinces> result = new List<Provinces>(); using (EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { var entities = context.DD_Provinces; foreach (EF.DD_Provinces entity in entities) { Provinces provinces = new Provinces() { ID = entity.ID, Province = entity.Province, ProvinceID = entity.ProvinceID }; result.Add(provinces); } } return result; } /// <summary> /// 获取所有县 /// </summary> /// <returns></returns> IList<Cities> IIndexDAO.GetAllCities() { IList<Cities> result = new List<Cities>(); using (EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { var entities = context.DD_Cities; foreach (EF.DD_Cities entity in entities) { Cities cities = new Cities() { City = entity.City, CityID = entity.CityID, ID = entity.ID, ParentID = entity.ParentID }; result.Add(cities); } } return result; } /// <summary> /// 获取所有市 /// </summary> /// <returns></returns> IList<Districts> IIndexDAO.GetAllDistricts() { IList<Districts> result = new List<Districts>(); using (EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { var entities = context.DD_Districts; foreach (EF.DD_Districts entity in entities) { Districts districts = new Districts() { District = entity.District, DistrictID = entity.DistrictID, ID = entity.ID, ParentID =entity.ParentID }; result.Add(districts); } } return result; } /// <summary> /// 根据省ID,获取省实体 /// </summary> /// <param name="provincesID"></param> /// <returns></returns> Provinces IIndexDAO.GetProvinces(int provincesID) { Provinces result = null; using (EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { var entity = context.DD_Provinces.Single(a => a.ProvinceID == provincesID); result = new Provinces() { ID = entity.ID, Province = entity.Province, ProvinceID = entity.ProvinceID }; } return result; } /// <summary> /// 根据城市ID,获取城市实体 /// </summary> /// <param name="districtID"></param> /// <returns></returns> Districts IIndexDAO.GetDistricts(int districtID) { Districts result = null; using (EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { var entity = context.DD_Districts.Single(a => a.DistrictID == districtID); result = new Districts() { District = entity.District, DistrictID = entity.DistrictID, ID = entity.ID, ParentID = entity.ParentID }; } return result; } /// <summary> /// 根据区县ID,获取区县实体 /// </summary> /// <param name="cityID"></param> /// <returns></returns> Cities IIndexDAO.GetCities(int cityID) { Cities result = null; using (EF.ddgwDBEntities context = new EF.ddgwDBEntities()) { var entity = context.DD_Cities.Single(a => a.CityID == cityID); result = new Cities() { City = entity.City, CityID = entity.CityID, ID = entity.ID, ParentID = entity.ParentID }; } return result; } #endregion
http://files.cnblogs.com/files/xbblogs/%E7%9C%81%E5%B8%82%E5%8E%BF.zip
时间: 2024-11-05 22:51:00