The most important thing is to judge who according to who wrote!!! Clear Ideas!!!
1, Modelsnamespacethe drop-down list linkage display _ Chinese provinces and cities _. models{ Public classCHINABF {PrivateMydbdatacontext _context =NewMydbdatacontext (); PublicList<chinastates>Select () {return_context. Chinastates.tolist (); } //because all the data is in one table, it's all based on Parentareacode and AreaCode comparison. PublicList<chinastates> Selectpro (stringAreaCode)//Provinces { var query = _context. Chinastates.where (P = P.parentareacode = = AreaCode); if(Query. Count () >0) { returnquery. ToList (); } Else { return NULL; } } PublicList<chinastates> SelectCity1 (stringAreaCode)//Municipal Jurisdiction { var query = _context. Chinastates.where (P = P.parentareacode = = AreaCode); if(Query. Count () >0) { returnquery. ToList (); } Else { return NULL; } } PublicList<chinastates> SelectCity2 (stringAreaCode)//Downtown { var query = _context. Chinastates.where (P = P.parentareacode = = AreaCode); if(Query. Count () >0) { returnquery. ToList (); } Else { return NULL; } } }}2, Controllersnamespacethe drop-down list linkage display _ Chinese provinces and cities _. controllers{ Public classHomecontroller:controller {[HttpGet]//when I first started loading the page, PublicActionResult Index () {//Province, according to number 0001, is the province. stringParentareacodepro; Parentareacodepro="0001"; List<ChinaStates> ListPro =NewCHINABF (). Selectpro (Parentareacodepro); SelectList BB=NewSelectList (ListPro,"AreaCode","AreaName"); Viewbag.databb=BB; //City jurisdiction Here is the default display of Beijing stringAreaCode1; AreaCode1=" One"; List<ChinaStates> listcity1 =NewCHINABF (). SelectCity2 (AREACODE1); SelectList CC1=NewSelectList (Listcity1,"AreaCode","AreaName"); VIEWBAG.DATACC1=cc1; //Beijing's data is displayed by default in the city . stringAreaCode; AreaCode="1101"; List<ChinaStates> listcity =NewCHINABF (). SelectCity2 (AreaCode); SelectList cc=NewSelectList (Listcity,"AreaCode","AreaName"); VIEWBAG.DATACC=cc; returnView (); } [HttpPost]//when the page is submitted, the data will change and the linkage display PublicActionResult Index ( string Pro,string City,string city1) three parameters are important, the name must be the same as the name value of the data in the views view { //Province or load number 0001 so all the provinces are loaded. stringAreacodepro; Areacodepro="0001"; List<ChinaStates> ListPro =NewCHINABF (). Selectpro (Areacodepro); SelectList BB=NewSelectList (ListPro,"AreaCode","AreaName", pro);//The fourth parameter is the selected value, which is selected to filter the cityViewbag.databb =BB; //Municipal JurisdictionList<ChinaStates> listcity1 =NewCHINABF (). SelectCity1 (PRO);//The value selected in the previous step to filter the city jurisdictionSelectList CC1 =NewSelectList (Listcity1,"AreaCode","AreaName", city); VIEWBAG.DATACC1=cc1; //Downtown var b = listcity1. Exists (P = P.areacode = = City)? City:listcity1[0]. AreaCode;//two kinds of situations, you need to judge whether the city's currentList<chinastates> listcity =NewCHINABF (). SelectCity2 (b); SelectList cc=NewSelectList (Listcity,"AreaCode","AreaName"); VIEWBAG.DATACC=cc; returnView (); } }}3, views@using drop-down list linkage display _ Chinese provinces and cities _. models;@{Layout=NULL;}<! DOCTYPE html>"Viewport"Content="Width=device-width"/> <title>Index</title>@{selectlist BB=Viewbag.databb; SelectList cc=VIEWBAG.DATACC; SelectList CC1=viewbag.datacc1; } @using (Html.BeginForm ("Index","Home", FormMethod.Post)) { <div>province: @Html. DropDownList ("Pro",BbNew{onchange="document.forms[0].submit ();"}) Municipal jurisdiction: @Html. DropDownList ("City",CC1,New{onchange="document.forms[0].submit ();"}) Urban area: @Html. DropDownList ("city1", CC)</div> }</body>
:
Pull-down list linkage display (Chinese provinces and cities) three-level linkage