Asp.net MVC4 下二级联动_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > Asp.net MVC4 下二级联动

Asp.net MVC4 下二级联动

 2014/7/4 18:56:04  浅水游鱼  程序员俱乐部  我要评论(0)
  • 摘要:效果图:主要文件有Index.cshtml,ErJLDController.cs,还有数据库文件。1、首先在数据库中创建省级、城市的表,我的表如下:我用了一张表放下了省级、城市的数据,用level划分省份和城市,parentId表示该城市所在省份的id1namespaceMvcproject.Controllers2{3//二级联动4publicclassErJLDController:Controller5{67ZjbEntitiesdb=newZjbEntities();8//9//GET
  • 标签:

效果图:

JSXxa1IiWCLtYWdUbk3VoO4xtzlwbYz7xThCazPeshZDznnIee9ZF1znJ0nbrTcF2s/+VP1/9zl3R+1VQuMlwTGi32Gb3v1116bYnaeuNGS1M7oAohv9E16xdXOaLDtrOe+rVJl5Qf2lVcyf7OeG62janHlPs6BMnF0/GRVMxJmb3A7dpq9wU3TxtKLyNGctb+ybKNkpq927iE7j50kFtdC0y5q2kXOuMgZF/nj4zg7j19/+mTC3TBsaJDYrgiM3/cZvuszfNOr/1uP7rLAGLGz7rWp+MfWhPPbWBjysv2cuKUSa1x28Uuw+GNlNauBiiu6yXq0jZ49tZeReK/ayZJDXLlvI5802VnGZWxC/Kp79vzSiorcEsK9GlI4KKWDUjpIpZOseRRnZ/7VJw/HnHdGbLVia82A5YbIfF1krhYaL/EN3/MNETtP/tjaLjOkmvHbFT3S8zLUSWhnrOkTNHqkG43fxOQ2x21rvsui+uZKjVCqSq2mUKr65kpNosk3Ou7otubb9WGTnZv+OCMYfUISuM3sfuDXXjvf2jllpxQOSukgFQ7yZrydf/mZn3f1Xyniywd9k6Zl7H9sx9E/Pltx6Wakgp47X3XufJVAOiOQzkQ+RwZWXLrZPz6LPdVffbB7dmbtVDgT1M6xOatAquobnU4W4jda2bxdYfRh/287iwUXZfAEjZ6g2kkZPUGj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L7cuhEW1gSLMqnl8ZVPvhjmEAF+x99rfn2U1hqSE0ZoQ7NwBssK8+jrzFdXoRKVxoyokU8BZXAB9sO0+cJZbW6YU1pFlFaj/SrKJbz8FOAA9sOwsJYoWmTTQyhpEhjIxhdOcF2AnggW1nMUGs0rQdITtCNoTsCN0DOwFMsO38nCACNO1GKBb3wU4AE2w7TxIESdNehGLRCHYCmEhspw+hWICdAC4S27mEUCx+BjsBTICdQOYCdgKZC9gJZC5gJ5C5gJ1A5gJ2ApkL2AlkLmAnkLmAnUDmAnYCmQvYCWQuYCeQuYCdQOYCdgKZy38AHVLCsLJuR8cAAAAASUVORK5CYII=" alt="" />

主要文件有Index.cshtml ,ErJLDController.cs ,还有数据库文件 。

1、首先在数据库中创建省级、城市的表,我的表如下:我用了一张表放下了省级、城市的数据,用level划分省份和城市,parentId表示该城市所在省份的id

QQAPYVSR85ccZWWQSsdtFJg7M8ZwlQHrXTQSgH7c4Yw1UErHbRSwP6cIUx10EoHrRSwP2cIUx200kErBezPmbXDNPMeSFehSeuglQL250wYpqGjLX1TLW9/vPTWc9BKoQX7OxuSmCNNI0yVdAaNj8xsCLvrcRlqWlHqKItlf5Q6atKO/ZFiJ0n+Kr2S/cU+KOZH2BVqWlHqKEsjrih1ZIL9ObOq/cX3sKbT5fuGO0pCKxJJGhhaoVNEqze/iB9jjv1ltsQf5UcGy3RPcOew7Y9WbYH9KbQ59cFNisHHHftb4zg7hD2eRVBZYH8Kbdof8ht8xLG/1Be72/vD+5Jgfwqbt79qiPry5CGDgBFxmCqjdfHYX+ognR37o9RRFuxPYfP2FzyggPnFrHrzWxmf8uBLh2d+KXWUx575pRnW4bFnZz7K1Ed+Y2fgUV4dtFLA/pwxb35TNHYzv7L0ILvrjDRpHbRSwP6cIUx10EoHrRSwP2cIUx200kErBUodOUOY6qCVDlopUOoIAHoKpY6cueIqLYNWOmilwNifM4SpDlrpoJUC9ucMYaqDVjpopYD9OUOY6qCVDlopYH/OEKY6aKWDVgrYnzONM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Z+ehSeuglQL25wxhqoNWOmilgP05Q5jqoJUOWilQ6sgZwlQHrXTQSoFSRwDQUyh15MwVV2kZtNJBKwXG/pwhTHXQSgetFLA/ZwhTHbTSQSsF7M8ZwlQHrXTQSgH7c4Yw1UErHbRSwP6cicO0SKTzA5q0DlopYH/ONMI0ftU3tZ56+bf136A9aNI6aKXgUuooSEDR+1QdYZgq7/k2PupV9pdak6bUUZa6/ZHyxcal1BEpFxeYN7+ZDh32N4NSR1kSWpFjroZLulPsb4F585vKbVVGrtdf+1tAkzZIaEXTq+Flf9y2zGjc/JojemW2smXfx/5wP4tIK0odGbgmu+e2Rev9lVZ3r+83vzMIIZt07w+5FvjaHxfupP3le38l9leWNOYMqZlf7n5DPOyvGoi9PHnY+79F6sGX1Dr2N4NSR1lCrSh1lMKj1FHwc9/Nb63eXzjG11v7o9RRnsbMb60Bwhwee3Ym89hz/GNp+V3fpz7AAq0UsD9nMlMf1Ra9u4f9wRS0UsD+nCFMddBKB60UsD9nCFMdtNJBKwVKHTlDmOqglQ5aKVDqCAB6yv8DXYWEeeynb44AAAAASUVORK5CYII=" alt="" />

class="code_img_closed" src="/Upload/Images/2014070418/0015B68B3C38AA5B.gif" alt="" />logs_code_hide('34b32d7e-a906-4374-a091-a65fe825ac4c',event)" src="/Upload/Images/2014070418/2B1B950FA3DF188F.gif" alt="" />
 1 namespace Mvcproject.Controllers
 2 {
 3     //二级联动
 4     public class ErJLDController : Controller
 5     {
 6 
 7         ZjbEntities db = new ZjbEntities();
 8         //
 9         // GET: /Test/
10         
11         public ActionResult Index()
12         {
13             //pro_city province=new pro_city();
14             
15             return View();
16         }
17 
18         public JsonResult getProvince() {
19             
20             List<pro_city> provinceList = (from p in db.pro_city where p.level == 1 select p).ToList();
21   
22 
23             JsonResult Jprovince = new JsonResult();
24             Jprovince.Data = provinceList;
25             Jprovince.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
26             return Jprovince;
27 
28         }
29 
30         public JsonResult getCity(string pName)
31         {
32 
33             //string pid = (from p in db.pro_city where p.areaValue == pName select p.id).ToString();
34             //int id = int.Parse(pid);
35             int id = int.Parse(pName);
36 
37             List<pro_city> cityList = (from p in db.pro_city where p.parentId == id select p).ToList();
38   
39             JsonResult Jcity = new JsonResult();
40             Jcity.Data = cityList;
41             Jcity.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
42             return Jcity;
43 
44         }
45        
46     }
47 }
ErJLDController.cs
 1 <body>
 2     <div>
 3         <select id="provinceId" >
 4          <option> 请选择省份</option>
 5         </select>
 6         <select id="cityId">
 7             <option>请选择市区</option>
 8         </select>
 9     </div>
10 
11     <script type="text/javascript">
12     
13         
14 
15         //用json从数据库里取一级列表的参数
16         $(function () {
17 
18             $.getJSON("ErJLD/getProvince/", function (obj) {
19                 $.each(obj, function (i, p) {
20                     $("#provinceId").append("<option value='"+p.id+"'>" + p.areaValue + "</option>");
21                 });
22 
23                 $("#provinceId").change(function () {
24                     //用attr()方法获取当前选择的option的value值(即p.id ,数据库里的id值,
25                     //虽然在TestController中的getCity方法中传入的是string类型的形参,但是后来需要变换成int类型, 所以value值应该为数字)
26                     var pName = $("#provinceId").attr("value");
27                     $.getJSON("ErJLD/getCity?pName=" + pName, getcity);
28                 });
29             });
30         });
31            
32       
33         
34 
35         function getcity(obj) {         
36             $("#cityId").empty();
37                         $.each(obj, function (m, v) {
38                             $("#cityId").append("<option >" + v.areaValue + "</option>");
39                         });
40        
41                     };
42                
43               
44             
45 
46     </script>
47 </body>
Index.cshtml

 

  • 相关文章
发表评论
用户名: 匿名