Asp.net MVC4 下二級聯動

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   檔案   

:

主要檔案有Index.cshtml ,ErJLDController.cs ,還有資料庫檔案 。

1、首先在資料庫中建立省級、城市的表,我的表如下:我用了一張表放下了省級、城市的資料,用level劃分省份和城市,parentId表示該城市所在省份的id

 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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.