本文在 http://www.cnblogs.com/darrenji/p/3606703.html(感谢博主的分享)基础上加入全国各省市,从文件中读取全国省市县,组成省市联动的选择标签
在Model里定义Province 和 City
public class Province
{
public int ID { get; set; }
public string Name { get; set; }
}
public class City
{
public int ID { get; set; }
public int ProvinceID { get; set; }
public string Name { get; set; }
public string ZipCode { get; set; }
}
在control里定义全局变量:
1 public static List<Province> prolist = new List<Province>(); 2 public static int pronum = 0; 3 public static List<City> citlist = new List<City>(); 4 public static int citnum = 0;
在control里读取文件生成prolist和citlist
1 public static void FileIO() 2 { 3 4 var path = System.Web.HttpContext.Current.Server.MapPath(@"~/Content/全国各省城市.txt"); 5 using (StreamReader sr = new StreamReader(path, System.Text.Encoding.Default)) 6 { 7 string s = ""; 8 while ((s = sr.ReadLine()) != null) 9 { 10 s = s.Trim(); 11 char[] str = s.ToCharArray() ; 12 if (str[0] >= '0' && str[0] <= '9' && (str[1] == '.' || (str[1] >= '0' && str[1] <= '9' && str[2] == '.'))) 13 { 14 int id; 15 string name; 16 if (str[1] == '.') 17 { 18 id = str[0] - '0'; 19 name = s.Substring(2, s.Length - 2); 20 } 21 else 22 { 23 id = (str[0] - '0') * 10 + str[1] - '0'; 24 name = s.Substring(3, s.Length - 3); 25 } 26 prolist.Add(new Province() { ID = id, Name = name }); 27 pronum++; 28 sr.ReadLine(); 29 } 30 else 31 { 32 string []ary; 33 ary = s.Split(' '); 34 for(int i=0;i<ary.Length;i++) 35 { 36 string it; 37 if (ary[i].Length == 1) { it = ary[i] + ary[i + 1]; i++; } 38 else 39 { 40 it=ary[i]; 41 } 42 citlist.Add(new City() { ID = citnum, Name = it, ProvinceID = pronum, ZipCode = citnum.ToString().PadLeft(4, '0') }); 43 citnum++; 44 } 45 } 46 } 47 } 48 }