:
The main files are index.cshtml, ErJLDController.cs, and database files.
1, first in the database to create provincial and city tables, my table is as follows: I used a table to put down the provincial and city data, the level of the provinces and cities, ParentID represents the city's province's ID
1 namespace Mvcproject.controllers2 {3 //Two-level linkage4 Public class Erjldcontroller:controller5 {6 7 zjbentities db = new zjbentities ();8 //9 //GET:/test/Ten One Public ActionResult Index () A { - //pro_city province=new pro_city (); - the return View (); - } - - Public Jsonresult getprovince () { + -List<pro_city>provincelist = (from P in db.pro_city where p.level = = 1 Select p). ToList (); + A at Jsonresult jprovince = new Jsonresult (); - jprovince.data = provincelist; - jprovince.jsonrequestbehavior = jsonrequestbehavior.allowget; - return jprovince; - - } in - Public Jsonresult getcity (string pName) to { + - //string pid = (from P in db.pro_city where p.areavalue = = PName Select p.id). ToString (); the //int id = Int. Parse (PID); * int id = Int. Parse (pName); $ Panax NotoginsengList<pro_city>citylist = (from P in db.pro_city where p.parentid = = ID Select p). ToList (); - the Jsonresult jcity = new Jsonresult (); + jcity.data = citylist; A jcity.jsonrequestbehavior = jsonrequestbehavior.allowget; the return jcity; + - } $ $ } -}
ErJLDController.cs
1 <Body>2 <Div>3 <SelectID= "Provinceid" >4 <option>Please select a province</option>5 </Select>6 <SelectID= "Cityid">7 <option>Please select City</option>8 </Select>9 </Div>Ten One <Scripttype= "Text/javascript"> A - - the //use JSON to take a list of parameters from a database - $(function () { - - $.getjson ("erjld/getprovince/", function(obj) { + $.each (obj,function(i, p) { - $("#provinceId"). Append ("<option value= '"+p.id+"' >" +P.areavalue+ "</option>"); + }); A at $("#provinceId"). Change (function () { - //Use the attr () method to get the value of the currently selected option (that is, p.id, the ID value in the database, - //Although a string-type parameter is passed in the Getcity method in TestController, it is later required to be transformed into an int type, so value should be a number . - varPName= $("#provinceId"). attr ("value"); - $.getjson ("erjld/getcity?pname=" +PName, getcity); - }); in }); - }); to + - the * functiongetcity (obj) { $ $("#cityId"). empty ();Panax Notoginseng $.each (obj,function(M, v) { - $("#cityId"). Append ("<option >" +V.areavalue+ "</option>"); the }); + A }; the + - $ $ </Script> - </Body>
index.cshtml