js获取省市

前台代码

@{
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

js获取省市的相关文章

JS获取客户端IP地址、MAC和主机名的7个方法汇总

一.使用JS获取客户端IP的几个方法 方法一(只针对IE且客户端的IE允许AcitiveX运行,通过平台:XP,SERVER03,2000). 获取客户端IP代码: 1 function GetLocalIPAddr() { 2 var oSetting = null; 3 var ip = null; 4 try { 5 oSetting = new ActiveXObject("rcbdyctl.Setting"); 6 ip = oSetting.GetIPAddress; 7

JS获取客户端IP地址、MAC和主机名七种方法

一.使用JS获取客户端IP的几个方法方法一(只针对IE且客户端的IE允许AcitiveX运行,通过平台:XP,SERVER03,2000).获取客户端IP代码:<HTML><HEAD><TITLE>GetLocalIP</TITLE></HEAD><BODY>获取IP:<script language="JavaScript"> function GetLocalIPAddr(){ var oSetti

JS获取图片的缩略图,并且动态的加载多张图片

找了好多资料也没有找到该死的ie的解决办法,最后放弃了ie <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>js获取缩略图</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <st

用js获取页面颜色值怎么比较?

一般情况下,我们通过十六进制的方式设置页面颜色值 如#64e164 但当我们通过js获取这个dom颜色值的时候,返回的值却可能不是十六进制的,所以比较的时候需要分浏览器进行 在火狐和谷歌浏览器中,返回的是rgb格式表示的颜色值,如前面的#64e164  返回的是  rgb(100, 225, 100) 这时候就需要用rgb格式的字符串和获取的值作比较才会返回true 但是在ie中,则需要使用16进制的方式比较才能返回true

js获取单选按钮的值

<!DOCTYPE html> <html> <body> <script type="text/javascript"> function select_patten(value) { // varvalue=document.getElementsByName(text).value; //  alert('hello'); alert(value); } </script> //以下这种方式只能获得单个单选按钮的valu

js selection省市2级级联菜单

主要分html部分和js部分 ,函数的参数为空时就是初始化状态,若要加载从后台读取数据的数据给option一个选中的初始状态的话,就可以给定参数即可,一下案例中给定了参数. city.html 页面 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <

js获取当前日期方法

我们经常会用到日期,下面是js获取日期的方法,后面有其他的方法还会不定期更新. 1 // 获取当前日期时间 2 function getCurrentDateTime(){ 3 var date=new Date(); 4 var year=date.getFullYear(); 5 var month=date.getMonth()+1; 6 var day=date.getDate(); 7 var hours=date.getHours(); 8 var minutes=date.getM

js获取时间

Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1970-????)myDate.getMonth();       //获取当前月份(0-11,0代表1月)myDate.getDate();        //获取当前日(1-31)myDate.getDay();         //获取当前星期X(0-6,0代表

js获取日期昨天,明天以及上月最后一天的日期

js回去今天,明天,昨天的日期 1 function getDateStr(AddDayCount) { 2 var dd = new Date(); 3 dd.setDate(dd.getDate() + AddDayCount); //获取AddDayCount天后的日期 4 var year = dd.getFullYear(); 5 var mon = dd.getMonth()+1; //获取当前月份的日期 6 var day = dd.getDate(); 7 return year