日期:2014-05-17  浏览次数:21062 次

ASP.NET MVC + Jquery 实现Ajax下拉框数据三级联动

这是第一使用Jquery实现城市三级数据联动的实例!之前本人没有使用过Jquery,在WebForm的开发过程中,很多时候用的是ASP.NET Ajax,ajaxPro +JavaScript,从未使用过JS框架,最近在学习ASP.NET MVC,在最新的发布版本中,已经集成了Jquery 1.3.1,自然,也要学一学!发现使用Jquery代码果然写得比原始JS优雅!只是语法和方法太多,现在还没弄熟,还需要时间来学习.....这次是第一次使用Jquery写客户端脚本,如果大家发现更好的实现方法,请给我留言!谢谢!

注明:ASP.NET MVC 1.0 ,作者:0x001;

View :

<script type="text/javascript">
??? $(document).ready(function() {
??????? GetByJquery();
??????? $("#ddlProvince").change(function() { GetCity() });
??????? $("#ddlCity").change(function() { GetDistrict() });
??? });
???
??? function GetByJquery() {

??????? $("#ddlProvince").empty(); //清空省份SELECT控件

??????? $.getJSON("/ajax/GetProvinceList", function(data) {
??????????? $.each(data, function(i, item) {
??????????????? $("<option></option>")
??????????????????? .val(item["ProvinceID"])
??????????????????? .text(item["ProvinceName"])
??????????????????? .appendTo($("#ddlProvince"));
??????????? });
??????????? GetCity();
??????? });?????

??? }

??? function GetCity() {

??????? $("#ddlCity").empty(); //清空城市SELECT控件
??????? var url ="/ajax/GetCityList/" + $("#ddlProvince").val();
??????? $.getJSON(url, function(data) {
??????????? $.each(data, function(i, item) {
??????????????? $("<option></option>")
??????????????????? .val(item["CityID"])
??????????????????? .text(item["CityName"])
??????????????????? .appendTo($("#ddlCity"));
??????????? });
??????????? GetDistrict();
??????? });
??? }

??? function GetDistrict() {
??????? $("#ddlDistrict").empty(); //清空市区SELECT控件
??????? var url = "/ajax/GetDistrictList/" + $("#ddlCity").val();
???????
??????? $.getJSON(url, function(data) {
??????????? $.each(data, function(i, item) {
??????????????? $("<option></option>")
??????????????????? .val(item["DistrictID"])
??????????????????? .text(item["DistrictName"])
??????????????????? .appendTo($("#ddlDistrict"));
??????????? });
??????? });
??? }

</script>
<table><tr><td><select id="ddlProvince"/></td><td><select id="ddlCity"/></td><td><select id="ddlDistrict" /></td></tr></table>

Controller :

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

namespace MvcBBS.Controllers
{
??? public class AjaxController : Controller
??? {
??????? //
??????? // GET: /Ajax/

??????? /// <summary>
??????? /// 获取所有[省份]数据
??????? /// </summary>
??????? public ActionResult GetProvinceList()
??????? {
??????????? if (!Request.IsAjaxRequest())
??????????? {
??????????????? return Content("请不要非法方法,这是不道德的行为!");
??????????? }

??????????? BLL.Province bll = new MvcBBS.BLL.Province();
??????????? List<MvcBBS.Model.S_Province> modellist = bll.GetProvinceList();

??????????? return Json(modellist);
??????? }

??????? /// <summary>
??????? /// 获取某[省份]的所有[城市]数据
??????? /// </summary>
??????? public ActionResult GetCityList(int id)
??????? {
??????????? if (!Request.IsAjaxRequest())
??????????? {
??????????????? return Content("请不要非法方法,这是不道德的行为!");
??????????? }
??????????? BLL.Province bll = new MvcBBS.BLL.Province();
??????????? List<MvcBBS.Model.S_City> modellist = bll.GetCityList(id);
??????????? return Json(modellist);
??????? }

??????? /// <summary>
??????? /// 获取某[城市]的所有[市区]数据
??????? /// </summary>
??????? public ActionResult GetDistrictList(int id)
??????? {
?