大佬教程收集整理的这篇文章主要介绍了MVC二级联动使用$.getJSON方法,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本篇使用jQuery的$.getJSON()实现二级联动。
□ View Models
1: namespace MvcApplication1.Models
2: {
3: public class province
4: {
5: int ID { get; set; }
6: String Name { get; set; }
7: }
8:
9: class City
10: {
11: 12: int provinceID { get; set; }
13: String Name { get; set; }
14: String ZipCode { get; set; }
15: }
16: }
17:
2: using System.Linq;
3: using MvcApplication1.Models;
4:
5: namespace MvcApplication1
6: {
7: static class service
8: {
9: static List<province> GetProvices()
10: {
11: List<province> result = new List<province>();
12: result.Add(new province(){ID = 1,Name = "山东省"});
13: result.Add(new province(){ID = 2,128)">"江苏省"});
14: return result;
15: }
16:
17: static List<City> GetCitiesByprovince(int provinceId)
18: {
19: List<City> result = new List<City>();
20: result.Add(new City(){ID=1,128)">"济南市",provinceID = 1,ZipCode = "001"});
21: result.Add(new City() { ID = 2,128)">"青岛市",128)">"002"});
22: result.Add(new City() { ID = 3,128)">"南京市",provinceID = 2,128)">"003" });
23: result.Add(new City() { ID = 4,128)">"苏州市",128)">"004" });
24:
25: return result.Where(r => r.provinceID == provinceId).ToList();
26: }
27: }
28: }
29:
□ HomeController
遍历集合,以List<SELEctListItem>返回给前台视图。
using System.Collections.Generic;
using System.Web.Mvc;
4: using MvcApplication1.Models;
5:
6: namespace MvcApplication1.Controllers
7: {
8: class HomeController : Controller
9: {
10: public ActionResult Index()
11: {
12: return View();
13: }
14:
15: public JsonResult Getprovinces()
16: {
17: List<SELEctListItem> items = new List<SELEctListItem>();
18: var provinces = service.GetProvices();
19: foreach (province p in provinces)
20: {
21: items.Add(new SELEctListItem()
22: {
23: Text = p.Name,
24: Value = Convert.ToString(p.ID)
25: });
26: }
27: if (!items.Count.Equals(0))
28: {
29: items.Insert(0,new SELEctListItem(){Text = "请选择",Value = ""});
30: }
31: return Json(items,JsonrequestBehavior.AllowGet);
32: }
33:
34: public JsonResult GetCities(String id)
35: {
36: List<SELEctListItem> items = new List<SELEctListItem>();
37: if (!String.IsNullOrEmpty(id))
38: {
39: var cities = service.GetCitiesByprovince(int.Parse(id));
40: foreach (City c in cities)
41: {
42: items.Add(new SELEctListItem()
43: {
44: Text = String.Concat(c.ZipCode," ",c.Name),
45: Value = c.ID.ToString()
46: });
47: }
48: if (!items.Count.Equals(0))
49: {
50: items.Insert(0,128)">""});
51: }
52: }
53: 54: }
55:
56: }
57: }
58:
□ Home/Index.cshtml视图
2: ViewBag.title = "Index";
3: Layout = "~/Views/Shared/_Layout.cshtml";
4: }
6: 选择省:<SELEct id="p"></SELEct> <br/>
7: 选择市:<SELEct id="c"></SELEct>
9: @section scripts
10: {
11: <script type="text/javascript">
12: $(function() {
13: getprovince();
15: $('#p').change(function() {
16: changeCity();
17: });
18: });
19:
20: //加载省
21: function getprovince() {
22: $.getJSON('@Url.Action("Getprovinces","Home")',function (data) {
23: $('#p').empty();
24: $.each(data,function(i,item) {
25: $('#p').append($('<option></option>').val(item.value).text(item.Text));
26: });
27: });
28: }
29:
30: //设置城市清空
31: function emptyCity() {
32: $('#c').empty();
33: $('#c').append($('<option></option>').val('').text('请选择'));
34: }
35:
36: //根据省加载城市
37: function changeCity() {
38: var SELEctedprovinceId = $.trim($('#p option:SELEcted').val());
39: if (SELEctedprovinceId.length == 0) {
40: emptyCity();
41: } else {
42: $.getJSON('@Url.Action("GetCities",{ id: SELEctedprovinceId },96)"> 43: $('#c').empty();
44: $.each(data,96)"> 45: $( 47: });
48: }
49: }
50: </script>
51: }
52:
结果:
以上是大佬教程为你收集整理的MVC二级联动使用$.getJSON方法全部内容,希望文章能够帮你解决MVC二级联动使用$.getJSON方法所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。