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
js获取省市的更多相关文章
- JS获取客户端IP地址、MAC和主机名七种方法
一.使用JS获取客户端IP的几个方法方法一(只针对IE且客户端的IE允许AcitiveX运行,通过平台:XP,SERVER03,2000).获取客户端IP代码:<HTML><HEAD ...
- JS获取客户端IP地址、MAC和主机名的7个方法汇总
今天在搞JS(javascript)获取客户端IP的小程序,上网搜了下,好多在现在的系统和浏览器中的都无效,很无奈,在Chrome.FireFox中很少搞到直接利用ActiveX获取IP等的JS脚本. ...
- js获取给定月份的N个月后的日期
1.在讲js获取给定月份的N个月后的日期之前,小颖先给大家讲下getFullYear().getYear()的区别. ①getYear() var d = new Date() console.log ...
- 【转】javascript浏览器参数的操作,js获取浏览器参数
原文地址:http://www.haorooms.com/post/js_url_canshu html5修改浏览器地址:http://www.cnblogs.com/JiangXiaoTian/ar ...
- js获取当前域名、Url、相对路径和参数以及指定参数
一.js获取当前域名有2种方法 1.方法一 var domain = document.domain; 2.方法二 var domain = window.location.host; 3.注意问题 ...
- js获取当前系统时间
Js获取当前日期时间及其它操作var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份 ...
- Js获取当前日期时间及其它操作
Js获取当前日期时间及其它操作var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份 ...
- js获取cookie
js获取cookie 之前用jQuery.cookie来获取cookie,虽然简单,但是项目上又多引用了一个插件,总觉得不太好,下面是我封装的js原生获取cookie的函数. function get ...
- js获取URL中的参数
js获取URL中的一些参数的意思 location对象 含有当前URL的信息. 属性 href 整个URL字符串. protocol 含有URL第一部分的字符串,如http: host 包含有URL中 ...
随机推荐
- Object Detection: Face Detection using Haar Cascades
目录 利用基于Haar特征的级联分类器实现人脸检测:官方教程 目标 学习基于Haar特征的级联分类器(Cascade Callifiers)实现人脸检测: 扩展到人眼检测: 基础知识 Paul V ...
- linux惊群
基本概念:子进程继承父进程环境和上下文的大部分内容的拷贝,其中就包括文件描述符表. 父进程fork出来的子进程,复制父进程的文件描述符.这些文件描述符fd是独立的,但是文件描述符指向的系统文件表项是唯 ...
- SpriteBuilder 不能对设置spriteframe的sprite进行设置dynamic Physics解决办法
可能spriteBuilder是最新推出 cocos2d 可视化,在学习过程中遇到一些Bug,比如你对一个精灵设置了一个动画帧(spriteframe),这并不会改变他的物理属性,正常来说是可以设置他 ...
- PAT天梯赛L2-005 集合相似度
题目链接:点击打开链接 给定两个整数集合,它们的相似度定义为:Nc/Nt*100%.其中Nc是两个集合都有的不相等整数的个数,Nt是两个集合一共有的不相等整数的个数.你的任务就是计算任意一对给定集合的 ...
- Centos7安装MySQL8.0
请到这个地址看:https://www.cnblogs.com/kevingrace/p/10482469.html Centos7安装MySQL8.0 - 操作手册 一.yum安装方式: 卸载之前版 ...
- javascript 中function(){}(),new function(),new Function(),Function
和java比起来,javascript真的是松散的无以复加,不过这也让我们在无聊之余,有精力去探讨一些复杂的应用,从而在开发之路上,获得一些新的想法. javascript中的类的构造 javascr ...
- spring自定义 xsd 位置
与元素类型 "beans" 相关联的属性 "xsi:schemaLocation" 的前缀 "xsi" 未绑定. 解决办法:(绿色字体) & ...
- LCA最近公共祖先模板(求树上任意两个节点的最短距离 || 求两个点的路进(有且只有唯一的一条))
原理可以参考大神 LCA_Tarjan (离线) TarjanTarjan 算法求 LCA 的时间复杂度为 O(n+q) ,是一种离线算法,要用到并查集.(注:这里的复杂度其实应该不是 O(n+q) ...
- mysql+gtid主从同步
安装mysql yum install mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-common-5.7.17-1. ...
- WireShark抓包分析(二)
简述:本文介绍了抓包数据含义,有TCP报文.Http报文.DNS报文.如有错误,欢迎指正. 1.TCP报文 TCP:(TCP是面向连接的通信协议,通过三次握手建立连接,通讯完成时要拆除连接,由于TCP ...