MVC和WebForm是微软B/S端的两条腿,两种不同的设计理念,相对来说MVC更优于WebForm对于大数据的交互,因为WebForm是同一时间传输所有数据,而MVC它只是传输所用到的数据,更精确,传输量少等待数据传输的响应时间就短.但是WebForm也有他的优点,比如说设计起来更像Winform容易理解.

SQL表如下:

根据ParentAreaCode=0001可以查出省级地市, 对应的省级地市有AreaCode

根据不同的AreaCode输入在ParentAreaCode中可以查出省级地市中的市级地市ParentAreaCode=37

同理,对应的AreaCode输入在ParentAreaCode中可以查出市级地市的区县ParentAreaCode=370303

________________________________________________________我是华丽的分割线________________________________________________

MVC:

M:Models, 模型层:可以放方法,类等处理数据.

V:Views,视图层:HTML页面,把数据装饰然后显示到页面给用户.

C:Controller,控制层:负责动作交互,从视图层获取信息给模型层,Vice versa.

新建项目ASP.NET.MVC 4 Web 应用程序选择空窗体,一个空的MVC空网页就有了.

首先在Models里面添加LinQ to SQL类 添加模型,选好数据库来源,拖入表

然后在Models里面添加一个空的类用来写代码.

 public class ChinaBF
{
private ChinaStatesDataContext _context = new ChinaStatesDataContext(); //LinQ查询数据库
public List<ChinaStates> Select(string AreaCode) //省
{
var query = _context.ChinaStates.Where(P => P.ParentAreaCode == AreaCode);
return query.ToList();
}
}

Controller:

 public class HomeController : Controller
{ [HttpGet] //刚开始加载页面的时候
public ActionResult Index()
{
List<ChinaStates> listpro = new ChinaBF().Select(""); //0001查省份
ViewBag.databb = new SelectList(listpro, "AreaCode", "AreaName");
List<ChinaStates> list1 = new ChinaBF().Select(""); //市
ViewBag.datacc1 = new SelectList(list1, "AreaCode", "AreaName");
List<ChinaStates> list2 = new ChinaBF().Select("");//区
ViewBag.datacc= new SelectList(list2, "AreaCode", "AreaName");
return View();
}
[HttpPost] //当页面提交后
public ActionResult Index(string pro,string city,string sub)
{
List<ChinaStates> listpro = new ChinaBF().Select("");
ViewBag.databb = new SelectList(listpro, "AreaCode", "AreaName",pro);
List<ChinaStates> list1 = new ChinaBF().Select(pro);
ViewBag.datacc1 = new SelectList(list1, "AreaCode", "AreaName",city);
var b = list1.Exists(P => P.AreaCode == city) ? city : list1[].AreaCode;
List<ChinaStates> list2 = new ChinaBF().Select(b);
ViewBag.datacc= new SelectList(list2, "AreaCode", "AreaName");
return View();
} }

Views:

视图要在Controller里面代码快区域右键 添加视图

用razor视图方法来写Views,@{} 里面写C#代码,他会自动判定HTML和C# 当遇到<>自动转译到Html.

@using China.Models;
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@{
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("sub",cc)
</div>
}
</div>
</body>
</html>

________________________________________________________我是华丽的分割线________________________________________________

WebForm:

public class ChinaStatesDA
{
private ChinaThreeDataContext context;
public ChinaStatesDA()
{
context = new ChinaThreeDataContext();
}
public List<ChinaStates> Ch(string ParentAreaCode)
{
return context.ChinaStates.Where(p => p.ParentAreaCode == ParentAreaCode).ToList();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Home : System.Web.UI.Page
{ protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) //因为每一次提交页面就会刷新, IsPostBack就判断是否重复刷新
{
bind();
}
} public void bind()
{
List<ChinaStates> list = new ChinaStatesDA().Ch("");
foreach (ChinaStates data in list)
{
ListItem li = new ListItem(data.AreaName,data.AreaCode);
DropDownList1.Items.Add(li);
}
}
public void bindDrop()
{
List<ChinaStates> list =new ChinaStatesDA().Ch("");
DropDownList1.DataSource = list;
DropDownList1.DataTextField = "AreaName";
DropDownList1.DataValueField = "AreaCode";
DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ListItem li = new ListItem("请选择");
string a = DropDownList1.SelectedItem.Value;//DropDownList1.SelectedValue.ToString();
List<ChinaStates> list=new ChinaStatesDA().Ch(a);
DropDownList2.DataSource = list;
DropDownList2.DataTextField = "AreaName";
DropDownList2.DataValueField = "AreaCode";
DropDownList2.DataBind();
DropDownList3.DataTextField = li.Text;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string a = DropDownList2.SelectedItem.Value;//DropDownList1.SelectedValue.ToString();
List<ChinaStates> list = new ChinaStatesDA().Ch(a);
DropDownList3.DataSource = list;
DropDownList3.DataTextField = "AreaName";
DropDownList3.DataValueField = "AreaCode";
DropDownList3.DataBind();
}
}

DropDownList 要选 AutoPostBack=True;这样list里面更改数据后他会自动提交

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 218px"> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
省<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
市<asp:DropDownList ID="DropDownList3" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
</asp:DropDownList>
区</div>
</form>
</body>
</html>

MVC和WebForm 中国省市区三级联动的更多相关文章

  1. java的JCombobox实现中国省市区三级联动

    源代码下载:点击下载源代码 用xml存储中国各大城市的数据. xml数据太多了就不贴上了,贴个图片: 要解释xml,添加了一个jdom.jar,上面的源代码下载里面有. 解释xml的类: packag ...

  2. Webform——中国省市三级联动以及IsPostBack

    首先要明白Webform的运行顺序,当开始启动时候,首先执行的是Page_Load事件, 当点击任意按钮后,每次点击都要先执行一遍Page_Load(在这里Page_Load里面的事件是给数据控件加载 ...

  3. [moka同学转载]Yii2 中国省市区三级联动

    1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...

  4. Yii2 中国省市区三级联动

    1.获取源码:https://github.com/chenkby/yii2-region 2.安装 添加到你的composer.json文件 "chenkby/yii2-region&qu ...

  5. laraveladmin省市区三级联动

    Distpicker是一个中国省市区三级联动选择组件,这个包是基于Distpicker的laravel-admin扩展,用来将Distpicker集成进laravel-admin的表单中 安装 com ...

  6. JS实现年月日三级联动+省市区三级联动+国家省市三级联动

    开篇随笔:最近项目需要用到关于年月日三级联动以及省市区三级联动下拉选择的功能,于是乎网上搜了一些做法,觉得有一些只是给出了小的案例或者只有单纯的js还不完整,却很难找到详细的具体数据(baidu搜索都 ...

  7. jQuery省市区三级联动插件

    体验效果:http://hovertree.com/texiao/bootstrap/4/支持PC和手机移动端. 手机扫描二维码体验效果: 代码如下: <!DOCTYPE html> &l ...

  8. 省市区三级联动 pickerView

    效果图 概述 关于 省市区 三级联动的 pickerView,我想大多数的 iOS 开发者应该都遇到过这样的需求.在遇到这样的需求的时候,大多数人都会觉的这个很复杂,一时无从下手.其实真的没那么复杂. ...

  9. JS省市区三级联动

    不需要访问后台服务器端,不使用Ajax,无刷新,纯JS实现的省市区三级联动. 当省市区数据变动是只需调正js即可. 使用方法: <!DOCTYPE html><html>< ...

随机推荐

  1. python标准模块(三)

    本文会涉及到的模块: subprocess logging 1. subprocess 可以执行shell命令的相关模块和函数有: os.system os.spawn os.popen --废弃 p ...

  2. Linux下查看软件的安装路径

    一.which 命令 Shell 的which 命令可以找出相关命令是否已经在搜索路径中. $ which git/usr/bin/git 二.whereis 命令 whereis 命令搜索更大范围的 ...

  3. WinForm------GridControl添加底部合计框

    1.在GridView属性中找到"OptionsView" => "ShowFooter" = true 2.打开编辑器,如图 . 3.获取统计数据(注意 ...

  4. 平面内,线与线 两条线找交点 两条线段的位置关系(相交)判定与交点求解 C#

    个人亲自编写.测试,可以正常使用   道理看原文,这里不多说   网上找到的几篇基本都不能用的   C#代码 bool Equal(float f1, float f2) { return (Math ...

  5. javascript 高级程序设计 -有感

    本来我想写一个高级程序设计总结的,结果发现我进入了一扇门,里面所有的字都要逐字逐句的理解,所有描述已经是非常精炼了,我最初的想法无异于老鼠吃大象. 我现在记录的是我在看这本时的感想. 2015.4月9 ...

  6. Windows群集安装

    一.安装前准备 1.安装dotnet 3.5 框架功能 2.安装starwind,并创建虚拟磁盘http://www.cnblogs.com/chhuang/p/3623305.html 3.使用iS ...

  7. JavaScript -- 小试牛刀

    //var a = parseInt(window.prompt("请输入一个数字!","")); //switch(a) { // case 1 : // c ...

  8. 关于当传过来的值转换成string类型报错的问题

    有时候直接写 string str=request.param["str"].tostring;会报错,是因为接受到的值可能是空的 这个时候就可以这样写 string _actio ...

  9. PHP如何获取二个日期的相差天数?

    我们经常需要获取二个日期之间相差的天数,方便客户知道距离某个时间段是相差了多少天数,这样的显示结果现在是越来越流行的了.不再像以前那样呆板的显示日期的了.我们这里就分享了二种方法可以获取到二个日期之间 ...

  10. PHP流式上传和表单上传(美图秀秀)

    最近需要开发一个头像上传的功能,找了很多都需要授权的,后来找到了美图秀秀,功能非常好用. <?php /** * Note:for octet-stream upload * 这个是流式上传PH ...