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中 ...
随机推荐
- 正经学C#_判断[IF语句]:[c#入门经典]
判断语句几乎是最为常用的语句之一,是最有效的. 先说IF语句,IF语句也是属于分支的一种,用来控制流程的. IF的语句是这样的 IF(xxx 条件) { //代码块 } ,b; ) { b=a--; ...
- Git入门操作
仅学习Git的一些入门操作比较容易,平时更多地使用GitHub,不过今天我想自个搭个服务练练手.当看完一些材料合作了一些验证之后,才发现其实所谓的服务和之前的svn完全不一样了.过程记录如下: Lin ...
- hdu1403(后缀数组模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1403 题意: 给出两个字符串, 求他们的最长公共子串 思路: 两个字符串的最长公共子串长度显然就是两个 ...
- SAP ABAP ALV构建动态输出列与构建动态内表(包留备用),包含操作abap元类型表及类
https://blog.csdn.net/zhongguomao/article/details/51095946
- 2、OpenCV Python 图像属性获取
__author__ = "WSX" import cv2 as cv import numpy as np image = cv.imread("1.JPG" ...
- .net mvc 框架实现后台管理系统 2
layui 数据表格 返回格式: var json = new { code = 0, count = pagers.totalRows, msg = "", data =null ...
- 树莓派-开启spi
1. sudo raspi-config #进入树莓派配置页 2. #进入每5项,进入启用spi即可
- redis启动后出现"WARNING you have Transparent Huge Pages (THP) support enabled in your kernel"问题
问题描述:启动redis后出现:WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This w ...
- 【2014年百度之星资格赛1001】Energy Conversion
Problem Description 魔法师百小度也有遇到难题的时候—— 现在,百小度正在一个古老的石门面前,石门上有一段古老的魔法文字,读懂这种魔法文字需要耗费大量的能量和大量的脑力. 过了许久, ...
- ubuntu下vnc部署安装
ubuntu下vnc部署安装,参考如下博客:https://www.cnblogs.com/xuliangxing/p/7642650.html https://jingyan.baidu.com/a ...