最主要的就是要判断谁根据谁编写的!!!理清思路!!!
1、Models namespace 下拉列表联动显示_中国省市_.Models { public class ChinaBF { private MyDBDataContext _context = new MyDBDataContext(); public List<ChinaStates> Select() { return _context.ChinaStates.ToList(); } //因为所有数据都在一个表里, 都是根据ParentAreaCode与AreaCode比较 public List<ChinaStates> SelectPro(string AreaCode) //省份 { var query = _context.ChinaStates.Where(P => P.ParentAreaCode == AreaCode); if (query.Count() > 0) { return query.ToList(); } else { return null; } } public List<ChinaStates> SelectCity1(string AreaCode)//市辖 { var query = _context.ChinaStates.Where(P => P.ParentAreaCode == AreaCode); if (query.Count() > 0) { return query.ToList(); } else { return null; } } public List<ChinaStates> SelectCity2(string AreaCode)//市区 { var query = _context.ChinaStates.Where(P => P.ParentAreaCode == AreaCode); if (query.Count() > 0) { return query.ToList(); } else { return null; } } } } 2、Controllers namespace 下拉列表联动显示_中国省市_.Controllers { public class HomeController : Controller { [HttpGet] //刚开始加载页面的时候 public ActionResult Index() { //省份 根据编号0001查的就是省份 string ParentAreaCodepro; ParentAreaCodepro = "0001"; List<ChinaStates> listpro = new ChinaBF().SelectPro(ParentAreaCodepro); SelectList bb = new SelectList(listpro, "AreaCode", "AreaName"); ViewBag.databb = bb; //市辖 这里默认显示的是北京 string AreaCode1; AreaCode1 = "11"; List<ChinaStates> listcity1 = new ChinaBF().SelectCity2(AreaCode1); SelectList cc1 = new SelectList(listcity1, "AreaCode", "AreaName"); ViewBag.datacc1 = cc1; //市区 这里默认显示的是北京的数据 string AreaCode; AreaCode = "1101"; List<ChinaStates> listcity = new ChinaBF().SelectCity2(AreaCode); SelectList cc = new SelectList(listcity, "AreaCode", "AreaName"); ViewBag.datacc = cc; return View(); } [HttpPost] //当页面提交后,数据便会改变,联动显示 public ActionResult Index(string pro,string city,string city1)三个参数很重要,名字必须和Views视图里数据的name值一样 { //省份 还是加载编号0001 这样加载的全是省份 string AreaCodepro; AreaCodepro = "0001"; List<ChinaStates> listpro = new ChinaBF().SelectPro(AreaCodepro); SelectList bb = new SelectList(listpro, "AreaCode", "AreaName",pro); //第四个参数是选定的值,通过选定 的这个值来筛选市辖 ViewBag.databb = bb; //市辖 List<ChinaStates> listcity1 = new ChinaBF().SelectCity1(pro);//由上一步选定的值来筛选市辖 SelectList cc1 = new SelectList(listcity1, "AreaCode", "AreaName",city); ViewBag.datacc1 = cc1; //市区 var b = listcity1.Exists(P => P.AreaCode == city) ? city : listcity1[0].AreaCode;//两种情况,需要判断一下 看看市辖是不是当前的 List<ChinaStates> listcity = new ChinaBF().SelectCity2(b); SelectList cc = new SelectList(listcity, "AreaCode", "AreaName"); ViewBag.datacc = cc; return View(); } } } 3、Views @using 下拉列表联动显示_中国省市_.Models; @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> @{ SelectList bb = ViewBag.databb; SelectList cc = ViewBag.datacc; SelectList cc1 = ViewBag.datacc1; } @using (Html.BeginForm("Index", "Home", FormMethod.Post)) { <div> 省份: @Html.DropDownList("pro",bb,new { onchange="document.forms[0].submit();"}) 市辖:@Html.DropDownList("city",cc1,new { onchange="document.forms[0].submit();"}) 市区:@Html.DropDownList("city1",cc) </div> } </body> </html>
效果图: