MVC省市区三级下拉菜单联动

控制器端代码(都在同一个表中):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication10.Models;

namespace 下拉列表联动显示_中国省市_.Controllers
{
    public class HomeController : Controller
    {

        [HttpGet] //刚开始加载页面的时候
        public ActionResult Index()
        {

            //省份  根据编号0001查的就是省份
            string ParentAreaCodepro;
            ParentAreaCodepro = "0001";//ParentAreaCode = '0001'
            List<ChinaStates> listpro = new ChinaStatesDA().SelectPro(ParentAreaCodepro);
            SelectList bb = new SelectList(listpro, "AreaCode", "AreaName");
            ViewBag.databb = bb;

            //市辖  这里默认显示的是北京
            string AreaCode1;
            AreaCode1 = "11";//数据库中的 ParentAreaCode = '11'
            List<ChinaStates> listcity1 = new ChinaStatesDA().SelectPro(AreaCode1);
            SelectList cc1 = new SelectList(listcity1, "AreaCode", "AreaName");
            ViewBag.datacc1 = cc1;



            //市区 这里默认显示的是北京的数据
            string AreaCode;
            AreaCode = "1101";//数据库中的 ParentAreaCode = '1101'
            List<ChinaStates> listcity = new ChinaStatesDA().SelectPro(AreaCode);
            SelectList cc = new SelectList(listcity, "AreaCode", "AreaName");
            ViewBag.datacc = cc;
            return View();
        }

        [HttpPost]  //当页面提交后,数据便会改变,联动显示
        public ActionResult Index(string pro, string city, string city1)
        {

            //省份  还是加载编号0001 这样加载的全是省份
            string AreaCodepro;
            AreaCodepro = "0001";
            List<ChinaStates> listpro = new ChinaStatesDA().SelectPro(AreaCodepro);

            SelectList bb = new SelectList(listpro, "AreaCode", "AreaName", pro); //第四个参数是选定的值,通过选定 的这个值来筛选市辖
            ViewBag.databb = bb;

            //市辖

            List<ChinaStates> listcity1 = new ChinaStatesDA().SelectPro(pro);//由上一步选定的值来筛选市辖
            SelectList cc1 = new SelectList(listcity1, "AreaCode", "AreaName", city);
            ViewBag.datacc1 = cc1;

            //市区
            var b = listcity1.Exists(P => P.AreaCode == city) ? city : listcity1[0].AreaCode;//两种情况,需要判断一下 看看市辖是不是当前的
            List<ChinaStates> listcity = new ChinaStatesDA().SelectPro(b);
            SelectList cc = new SelectList(listcity, "AreaCode", "AreaName");
            ViewBag.datacc = cc;
            return View();

        }

    }
}

视图端代码:

@using MvcApplication10.Models;

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @{
        SelectList bb = ViewBag.databb;
        SelectList cc = ViewBag.datacc;
        SelectList cc1 = ViewBag.datacc1;
        }
     @using (Html.BeginForm("Index", "Home", FormMethod.Post))
       {
    <div>

        
        省份: @Html.DropDownList("pro",bb,new { οnchange="document.forms[0].submit();"})
        市辖:@Html.DropDownList("city",cc1,new { οnchange="document.forms[0].submit();"})
         市区:@Html.DropDownList("city1",cc)
    </div>
     }
</body>
</html>

Model层方法代码:

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

namespace MvcApplication10.Models
{
    public class ChinaStatesDA
    {
        private ChinaStatesDataContext _Context = new ChinaStatesDataContext();
        public List<ChinaStates> Select()
        {
            return _Context.ChinaStates.ToList();
        }

        //因为所有数据都在一个表里, 都是根据ParentAreaCode与AreaCode比较,ParentAreaCode查出来的AreaCode就等于下一级的ParentAreaCode
        public List<ChinaStates> SelectPro(string AreaCode) //省份
        {

            var query = _Context.ChinaStates.Where(P => P.ParentAreaCode == AreaCode);
            if (query.Count() > 0)
            {
                return query.ToList();
            }
            else
            {
                return null;
            }

        }


    }
}

 

转载于:https://www.cnblogs.com/dlexia/p/4649165.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值