码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-07-15 20:43:09      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

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

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 { onchange="document.forms[0].submit();"})
        市辖:@Html.DropDownList("city",cc1,new { onchange="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;
            }

        }


    }
}

 

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

标签:

原文地址:http://www.cnblogs.com/dlexia/p/4649165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!