On the internet for a long time, have not found the kind of effect that they want, and finally wrote it out, although the method is a bit stupid.
This is the controller.
1 PublicActionResult Edit (stringID)2 {3 //Inquire about shipping address information4 vardata =Addressdirectorylogic.getbyid (ID);5 6 //first find the provinces and put them in the viewbag.7 varProvinces =regionlogic.getchinaprovinces ();8List<selectlistitem> Pitems =NewList<selectlistitem>();9 foreach(varPinchProvinces)//get the province name and ID based on the listTen { OnePitems.add (NewSelectListItem {Text = p.name, Value =p.id}); A } -Viewbag.province =Pitems; - the //City - varCity =regionlogic.getcities (data. province); -list<selectlistitem> item =NewList<selectlistitem> ();//get a list of cities - foreach(varCinchCity//get city name and ID by list + { -Item. ADD (NewSelectListItem {Text = c.name, Value =c.id}); + } AViewbag.city =item; at //Save the province and city values in the shipping address information to show -Viewbag.provinceid =data. Province; -Viewbag.cityid =data. City; - - returnView (data); -}
Js
1 $ (document). Ready (function () {2 //getprovince ();//Loading provinces3 $ (' #T_Province '). Val (' @ViewBag. Provinceid ');4 $ (' #T_City '). Val (' @ViewBag. Cityid ');5 $ ("#T_Province"). Change (function () {getcity ()});//Load City6 });7 function Getprovince () {8 $ ("#T_Province"). empty ();//emptying the province select control9 $.getjson ("/addressdirectory/getprovincelist", function (data) {Ten $.each (data, function (I, item) { One$("<option></option>"). Val (item[" Id "]). Text (item[" Name "]). AppendTo ($ (" #T_Province ")); A }); - getcity (); - }); the } - function getcity () { - $ ("#T_City"). empty ();//Empty City Select control - $.getjson ("/addressdirectory/getcitylist", {pid: $ ("#T_Province"). Val ()}, + function (data) { - $.each (data, function (I, item) { +$("<option></option>"). Val (item[" Value "]). Text (item[" text "]). AppendTo ($ (" #T_City ")); A }); at }); -}
Html
@Html. Dropdownlistfor (Model=>model. Province, (List<SelectListItem>) viewbag.province,new {@class = "combobox", id = "T_province"}) @Html. dropdownlistfor (model = model. City, (List<SelectListItem>) viewbag.city,new {@class = "combobox", id = "T_city " })
The above part JS is referring to others. The following is part of the JS source code;
Source: http://blog.csdn.net/qq_17202783/article/details/43371421
MVC data echo for three-level linkage modification