1 <h2>Index</h2> 2 @*一级根节点*@ 3 @Html.DropDownList("YiJi", ViewBag.user as SelectList, new { onchange = "City()" }) 4 5 <select id="Select1" onchange="Citys()"> 6 <option>二级根节点</option> 7 </select> 8 9 10 <select id="Select2"> 11 <option>三级根节点</option> 12 </select> 13 14 15 <script> 16 //二级联动 17 function City() { 18 var i = $("#YiJi").val(); 19 $.ajax({ 20 url: "/Default/GetCity?id=" + i, 21 dataType: "json", 22 type: "get", 23 success: function (data) { 24 $("#Select1").empty().append("<option >二级根节点</option>") 25 $("#Select2").empty().append("<option >三级根节点</option>") 26 $(data).each(function () { 27 var str = '<option value="' + this.Id + '">' + this.Name + '</option>'; 28 $("#Select1").append(str); 29 }) 30 } 31 }) 32 33 } 34 //三级联动 35 function Citys() { 36 var i = $("#Select1").val(); 37 $.ajax({ 38 url: "/Default/GetCity?id=" + i, 39 dataType: "json", 40 type: "get", 41 success: function (data) { 42 $("#Select2").empty().append("<option >三级根节点</option>") 43 $(data).each(function () { 44 var str = '<option value="' + this.Id + '">' + this.Name + '</option>'; 45 $("#Select2").append(str); 46 }) 47 } 48 }) 49 50 } 51 </script>
CityBll bll = new CityBll(); /// <summary> /// 主页面 /// </summary> /// <returns></returns> public ActionResult Index() { var list = bll.getList().Where(m => m.PId == 0).ToList(); SelectList list2 = new SelectList(list, "Id", "Name"); ViewBag.user = list2; return View(); } /// <summary> /// 获取城市信息 /// </summary> /// <param name="id"></param> /// <returns></returns> public string GetCity(int id) { var list = bll.getList().Where(m => m.PId == id).ToList(); return JsonConvert.SerializeObject(list); }