• MVC 对中国省市区的联动查询代码。


    Model里的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Mvcshengshiqu.Models
    {
        public class ChinaBF
        {
            private masterDataContext _Context = new masterDataContext();
            public List<ChinaStates> Select()
            {
                return _Context.ChinaStates.ToList();
            }
           //因为所有的数据都在一个表里,所以通过编号查询身份,通过省份查询城市,或者通过城市查询地区
            public List<ChinaStates> Selectcity(string Areacode)
            {
                var query = _Context.ChinaStates.Where(p=>p.ParentAreaCode==Areacode);
                if (query.Count()>0)
                {
                    return query.ToList();
                }
                return null;
            } 
        }
    }

    控制器里的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Mvcshengshiqu.Models;
    
    namespace Mvcshengshiqu.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
              [HttpGet] 
            public ActionResult Index()
            {
                
                string ParentAreaCode = "0001";
                //通过编号001查询出的就是所有的省份
                List<ChinaStates> list = new ChinaBF().Selectcity(ParentAreaCode);
                ViewBag.Data1 = new SelectList(list,"AreaCode","AreaName");
    
                //默认显示北京
                string Areacode = "11";
                List<ChinaStates> list1 = new ChinaBF().Selectcity(Areacode);
                ViewBag.Data2 = new SelectList(list1,"AreaCode","AreaName");
    
                //默认显示北京的辖区
    
                string Areacode1 = "1101";
                List<ChinaStates> list2 = new ChinaBF().Selectcity(Areacode1);
                ViewBag.Data3 = new SelectList(list2,"AreaCode","AreaName");
    
                return View();
            }
    
             [HttpPost] 
            //当页面提交时,就会改变数据,联动显示
            public ActionResult Index(string sheng,string shi,string qu)
            {
                string ParentAreaCode = "0001";
                //通过编号0001把所有的省份查询出来
                List<ChinaStates> list = new ChinaBF().Selectcity(ParentAreaCode);
                //第四个值是选定的省,通过选定的省来改变市
                ViewBag.Data1 = new SelectList(list, "AreaCode", "AreaName",sheng);
    
                //第四个值是选定的市,通过选定的市来改变区
                List<ChinaStates> list1 = new ChinaBF().Selectcity(sheng);
                ViewBag.Data2 = new SelectList(list1, "AreaCode", "AreaName",shi);
    
                //判断一下市是不是当前的市。如果改一下省,市会改变,根据市改变区
                var b=list1.Exists(p=>p.AreaCode==shi)?shi:list1[0].AreaCode;
    
                //通过选定的市改变区
                List<ChinaStates> listcity = new ChinaBF().Selectcity(b);
                ViewBag.Data3 = new SelectList(listcity, "AreaCode", "AreaName");
                
    
                return View();
            }
        }
    }


    视图里的代码

    @using Mvcshengshiqu.Controllers;
    @using  Mvcshengshiqu.Models;
    @model List<ChinaStates>
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    </head>
    <body>
        @{
            SelectList aa = ViewBag.Data1;
            SelectList bb = ViewBag.Data2;
            SelectList cc = ViewBag.Data3;       
        }  
     
              @using(Html.BeginForm("Index","Home",FormMethod.Post))
              {
                    <div>
                  省:@Html.DropDownList("sheng", aa, new {onchange="document.forms[0].submit();" })
                  市:@Html.DropDownList("shi", bb, new { onchange="document.forms[0].submit();"})
                  区:@Html.DropDownList("qu",cc)
                   </div>          
              }  
       
    </body>
    </html>
  • 相关阅读:
    cpu进程调度---RT Throttling【转】
    Google 开发新的开源系统 Fuchsia
    牛人博客!!!各大招聘网站信息实时查询浏览【转】
    ARM多核处理器启动过程分析【转】
    Linux 下多核CPU知识【转】
    Android的init过程(二):初始化语言(init.rc)解析【转】
    In_interrupt( ) 和In_irq( )【转】
    jiffies溢出与时间先后比较-time_after,time_before【转】
    Linux系统的中断、系统调用和调度概述【转】
    linux中断的上半部和下半部 【转】
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4638531.html
Copyright © 2020-2023  润新知