MVC 三级联动

本篇实现中国省市区三级联动,先看最终效果:

初始状态:

当选择第1个Select,第2、3个Select可供选择: 

当选择第2个Select,第1个不变、第3个Select可供选择:

 

当选择第3个Select,第1、2个不变:

下面是代码:

控制器Controllers\HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using 省市区__三级联动.Models;

namespace 省市区__三级联动.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        [HttpGet] //刚开始加载页面的时候
        public ActionResult Index()
        {
            //省份  根据编号0001查的就是省份
          
            List<ChinaStates> listsheng = new ChinaDA().SelectBysheng("0001");
            ViewBag.shengs = new SelectList(listsheng, "AreaCode", "AreaName");


       
            List<ChinaStates> listshi = new ChinaDA().SelectByshi("11");
            ViewBag.shis = new SelectList(listshi, "AreaCode", "AreaName");

           
            List<ChinaStates> listqu = new ChinaDA().SelectByqu("1101");
            ViewBag.qus = new SelectList(listqu, "AreaCode", "AreaName");


            return View();
        }
        [HttpPost]  
        public ActionResult Index(string sheng, string shi, string qu)
        {
            
            List<ChinaStates> listsheng = new ChinaDA().SelectBysheng("0001");
            ViewBag.shengs = new SelectList(listsheng, "AreaCode", "AreaName",sheng);

            List<ChinaStates> listshi = new ChinaDA().SelectByshi(sheng);
            ViewBag.shis = new SelectList(listshi, "AreaCode", "AreaName",shi);

            var b = listshi.Exists(P => P.AreaCode == shi) ? shi : listshi[0].AreaCode;
            List<ChinaStates> listqu = new ChinaDA().SelectByqu(b);
            ViewBag.qus = new SelectList(listqu, "AreaCode", "AreaName");
           
            return View();
        }

    }
}

  模型:

首先要用linq连接数据库 然后建一个类开始写方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace 省市区__三级联动.Models
{
    public class ChinaDA
    {
        private ChinaDataContext _Context = new ChinaDataContext();
        public List<ChinaStates> Select()
        {
            return _Context.ChinaStates.ToList();
        }
        public List<ChinaStates> SelectBysheng(string AreaCode)
        {
            var query = _Context.ChinaStates.Where(p => p.ParentAreaCode == AreaCode);
            return query.ToList();
           
        }
        public List<ChinaStates> SelectByshi(string AreaCode)
        {
            var query = _Context.ChinaStates.Where(p => p.ParentAreaCode == AreaCode);
            return query.ToList();
        }
        public List<ChinaStates> SelectByqu(string AreaCode)
        {
            var query = _Context.ChinaStates.Where(p => p.ParentAreaCode == AreaCode);
            return query.ToList();
        }
    }
}

  视图:

@using 省市区__三级联动.Models
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Index", "Home", FormMethod.Post))
        {
    <div>
            @Html.DropDownList("sheng",ViewBag.shengs as SelectList,new { οnchange="document.forms[0].submit();"})
            @Html.DropDownList("shi",ViewBag.shis as SelectList, new { οnchange="document.forms[0].submit();"})
            @Html.DropDownList("qu",ViewBag.qus as SelectList)
       
    </div>
        }
</body>
</html>

  

 

转载于:https://www.cnblogs.com/Mr-xue/p/4636244.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值