Two-level linkage under ASP. MVC4

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.