多级联动实现,附源码。当前,部分代码是参与博客园其它网友。

新增,前台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $(function () {
        GetProvince(); //加载省份
        $("#Province").change(function () {
            GetCity();
        });
    });
    function GetProvince() {
        //        $("#Province").empty();
        $.getJSON(
            "/Home/GetProvincelist",
            function (data) {
 
                $.each(data, function (i, item) {
                    $("<option></option>").val(item["PID"]).text(item["ProvinceName"]).appendTo($("#Province"));
                })
            });
        //            GetCity();
    }
    function GetCity() {
        $("#City").empty();
        $.getJSON(
        "/Home/GetCitylist",
        { pid: $("#Province").val() },
        function (data) {
            $.each(data, function (i, item) {
                $("<option></option>").val(item["Value"]).text(item["Text"]).appendTo($("#City"));
 
            })
        });
    }
</script>
<div style="margin:50px 0">
    <span>省份:</span>
    <select id="Province">
        <option>请选择</option>
    </select>
 
    <span>市:</span>
    <select id="City">
        <option>请选择</option>
    </select>
</div>

后台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/// <summary>
/// 模拟省份数据
/// </summary>
/// <returns></returns>
public List<Province> Provincelist()
{
    List<Province> list = new List<Province>();
    list.Add(new Province() { PID = 1, ProvinceName = "广东" });
    list.Add(new Province() { PID = 2, ProvinceName = "北京" });
    list.Add(new Province() { PID = 3, ProvinceName = "上海" });
    list.Add(new Province() { PID = 4, ProvinceName = "河北" });
    list.Add(new Province() { PID = 5, ProvinceName = "贵州" });
    list.Add(new Province() { PID = 6, ProvinceName = "云南" });
 
    return list;
}
 
/// <summary>
/// 模拟城市数据
/// </summary>
/// <returns></returns>
public List<City> Citylist()
{
    List<City> cityList = new List<City>();
    cityList.Add(new City() { CID = 1, PID = 1, CityName = "广州" });
    cityList.Add(new City() { CID = 2, PID = 1, CityName = "深圳" });
    cityList.Add(new City() { CID = 3, PID = 1, CityName = "惠州" });
    cityList.Add(new City() { CID = 4, PID = 1, CityName = "湛江" });
    cityList.Add(new City() { CID = 5, PID = 2, CityName = "北京" });
    cityList.Add(new City() { CID = 6, PID = 3, CityName = "上海" });
    cityList.Add(new City() { CID = 7, PID = 4, CityName = "唐山市" });
    cityList.Add(new City() { CID = 8, PID = 4, CityName = "保定市" });
    cityList.Add(new City() { CID = 9, PID = 4, CityName = "张家口市" });
    return cityList;
}
 
 
/// <summary>
/// 获取省份
/// </summary>
public JsonResult GetProvincelist()
{
    var list = Provincelist();
    return Json(list, JsonRequestBehavior.AllowGet);
}
 
/// <summary>
/// 获取城市
/// </summary>
/// <param name="pid"></param>
/// <returns></returns>
public JsonResult GetCitylist(int pid)
{
    var citys = Citylist().Where(m => m.PID == pid).ToList();
 
    List<SelectListItem> item = new List<SelectListItem>();
 
    foreach (var City in citys)
    {
        item.Add(new SelectListItem { Text = City.CityName, Value = City.CID.ToString() });
    }
    return Json(item, JsonRequestBehavior.AllowGet);
}

  

编辑逻辑:

模型ProvinceViewModel:

1
2
3
4
5
6
7
8
9
10
11
12
public class ProvinceViewModel
{
    /// <summary>
    /// 省份ID
    /// </summary>
    public int PID { getset; }
 
    /// <summary>
    /// 城市ID
    /// </summary>
    public int CID { getset; }
}

前台:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $(function () {
        //GetProvince(); //加载省份
        GetCity();
        //省份变动,加载城市
        $("#Province").change(function () {
            GetCity();
        });
    });
    function GetProvince() {
        //        $("#Province").empty();
        $.getJSON(
            "/Home/GetProvincelist",
            function (data) {
 
                $.each(data, function (i, item) {
                    $("<option></option>").val(item["PID"]).text(item["ProvinceName"]).appendTo($("#Province"));
                })
            });
        //            GetCity();
    }
    function GetCity() {
        $("#City").empty();
        $.getJSON(
        "/Home/GetCitylist",
        { pid: $("#Province").val() },
        function (data) {
            $.each(data, function (i, item) {
                $("<option></option>").val(item["Value"]).text(item["Text"]).appendTo($("#City"));
 
            })
        });
    }
</script>
<div style="margin:50px 0">
    <span>省份:</span>
    @Html.DropDownListFor(model => model.PID, ViewBag.ProvinceList as IEnumerable<SelectListItem>, "=请选择="new { @class "form-control", id = "Province" })
 
 
    <span>市:</span>
    @Html.DropDownListFor(model => model.CID,ViewBag.CityList  as IEnumerable<SelectListItem>, "=请选择="new { @class "form-control", id = "City" })
</div>

 后台:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public ActionResult Edit()
{
    ViewBag.ProvinceList = Provincelist().Select(m => new SelectListItem()
    {
        Text = m.ProvinceName,
        Value = m.PID.ToString(),
        Selected = (m.PID == 4) //测试,默认让它绑定第四个
    }).ToList();
    ViewBag.CityList = Citylist().Select(m => new SelectListItem()
    {
        Text = m.CityName,
        Value = m.CID.ToString(),
        Selected = (m.CID == 8) //测试,默认让它绑定第四个
    }).ToList();
    return View();
}

  

实际上,增加和编辑是完成可以合成同一个页面的,这里就不演示了。

源代码下载:点击下载

asp.net MVC 下拉多级联动及编辑的更多相关文章

  1. .net MVC 下拉多级联动及编辑

    多级联动实现,附源码.当前,部分代码是参与博客园其它网友. 新增,前台代码: <script src="~/Scripts/jquery-1.10.2.js">< ...

  2. jQuery Ajax MVC 下拉框联动

    无刷新下拉框联动方法: Controllers代码 public JsonResult DH_Change(string DH_ID) { List<SelectListItem> Tea ...

  3. MVC 下拉框联动效果(单选)

    下拉框联动效果,我们以部门--职位为例,选择部门时,关联到该部门的职位.下拉框的写法就不多说了,详细请参照前文. 视图: 其中,dept是部门的属性,deptlist是部门下拉框的属性,job是职位的 ...

  4. ASP.NET MVC 下拉框的传值的两种方式

    以前使用WebForm变成时,下拉框传值只需直接在后台绑定代码就可以了.现在我们来看看在MVC中DropDownList是如果和接受从Controller传过来的值的. 第一种:使用DropDownL ...

  5. 【ASP.NET】 MVC下拉框联动

    这个case主要是我在做项目的时候遇到一个需要根据input控件输入的内容,动态填充dropdown list中的内容, 实现二者联动的需求.在搜索了一些资源后,这篇博客解决了我的问题,所以记录并转载 ...

  6. ASP.NET MVC 下拉的使用(ViewData传递)

    C#部分 public void GetViewData() { List<string> data = new List<string>(); data.Add(" ...

  7. ASP.NET MVC 下拉框的传值的两种方式(第二种方式未完成)

    控制器代码: public ActionResult Index() { List<SelectListItem> sli = new List<SelectListItem> ...

  8. ASP.NET MVC下的四种验证编程方式[续篇]

    在<ASP.NET MVC下的四种验证编程方式>一文中我们介绍了ASP.NET MVC支持的四种服务端验证的编程方式("手工验证"."标注Validation ...

  9. ASP.NET MVC下的四种验证编程方式

    ASP.NET MVC采用Model绑定为目标Action生成了相应的参数列表,但是在真正执行目标Action方法之前,还需要对绑定的参数实施验证以确保其有效性,我们将针对参数的验证成为Model绑定 ...

随机推荐

  1. A multiprocessing system including an apparatus for optimizing spin-lock operations

    A multiprocessing system having a plurality of processing nodes interconnected by an interconnect ne ...

  2. 夜话JAVA设计模式之策略模式

    策略模式     定义了算法簇,分别封装起来,让他们之间可以互相替换,让算法簇的变化独立于使用算法的客户.设计原则1     找出应用中可能需要变化之处,把他们独立出来,不要和那些不需要变化的代码混在 ...

  3. codevs——1530 大质数

    1530 大质数  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 小明因为没做作业而被数学老师罚站,之后数学老师 ...

  4. Hackerrank alien-flowers(数学公式)

    题意:求能够构造出的符合以下条件的字符串的数目 .字符串只由R和B组成且长度不为0 .字符串含有A个RR,B个RB,C个BB,D个BR A,B,C,D<=1e5 分析:最简单的方法就是dp,但是 ...

  5. [bzoj1821][JSOI2010]部落划分(贪心)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1821 分析:题目看起来很吊,但只要贪心就行了,每次取相邻最近的两个点所在的集合合并知道 ...

  6. 登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架

    登陆模块,这个是很重要的模块,有shiro和spring security专门的权限认证框架

  7. Node: Updating npm's bundled node gyp

    Linux, Mac OS X, Solaris, etc. Unix is easy. Just run the following command. Use sudo if necessary. ...

  8. Username is not in the sudoers file. This incident will be reported

    type sudo adduser <username> sudo where username is the name of the user you want to add in th ...

  9. STL 笔记(五) 算法 algorithm

    在 STL 中,算法是一系列的函数模版.STL 提供了大概 70 个算法,由头文件 <algorithm>.<numeric>.<functional>组成. 头文 ...

  10. 苹果iOS手机后门的”诊断功能论“不攻自破

    7月23日.苹果公司公布公告,题为"iOS:About diagnostic capabilities"("iOS:关于诊断功能").当中声称:iOS offe ...