MVC TIP6:级联菜单
来源:广州中睿信息技术有限公司官网
发布时间:2012/10/21 23:25:16 编辑:admin 阅读 392
级联菜单最有名的是省市级联,如果你还没有这样的数据库,请从这里下载Province.rar。1:MODEL准备3个Model,如下:publicclassProvince{publicintid{g

级联菜单最有名的是省市级联,如果你还没有这样的数据库,请从这里下载Province.rar

1:MODEL

准备3个Model,如下:

    public class Province      {          public int id { get; set; }          public string provinceID { get; set; }          public string province { get; set; }      }      public class Province      {          public int id { get; set; }          public string provinceID { get; set; }          public string province { get; set; }      }      public class Area      {          public int id { get; set; }          public string areaID { get; set; }          public string area { get; set; }          public string father { get; set; }      }

2:Controller部分的数据获取

如下:

    public class CityController : Controller      {          string conn = "Data Source=.;Initial Catalog=ForestFire;User Id=sa;Password=sa;";            public ActionResult Index()          {              return View("");          }            public ActionResult GetProvince()          {              if (!Request.IsAjaxRequest())              {                  return Content("请不要非法方法,这是不道德的行为!");              }              string sql = "select * from dbo.povince";              using (DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql))              {                  var provinces = DataTableHelper.DataTable2Objects<Province>(ds.Tables[0]);                  return Json(provinces, JsonRequestBehavior.AllowGet);                  //return Json(provinces);              }          }            public ActionResult GetCity(string id)          {              if (!Request.IsAjaxRequest())              {                  return Content("请不要非法方法,这是不道德的行为!");              }              string sql = "select * from dbo.city where father=@fatherID";              SqlParameter p1 = new SqlParameter("@fatherID", id);              using (DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, p1))              {                  var citys = DataTableHelper.DataTable2Objects<City>(ds.Tables[0]);                  return Json(citys, JsonRequestBehavior.AllowGet);                  //return Json(provinces);              }          }            public ActionResult GetArea(string id)          {              if (!Request.IsAjaxRequest())              {                  return Content("请不要非法方法,这是不道德的行为!");              }              string sql = "select * from dbo.area where father=@areaID";              SqlParameter p1 = new SqlParameter("@areaID", id);              using (DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, p1))              {                  var areas = DataTableHelper.DataTable2Objects<Area>(ds.Tables[0]);                  return Json(areas, JsonRequestBehavior.AllowGet);                  //return Json(provinces);              }          }        }

3:前台

如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">      Index.aspx  </asp:Content>  <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">      <script type="text/javascript">          $(document).ready(function () {              $("#getP").click(function () {                  GetByJquery();              });              $("#ddlProvince").change(function () { GetCity() });              $("#ddlCity").change(function () { GetArea() });          });            function GetByJquery() {              $("#ddlProvince").empty();               //                        $.getJSON("GetProvince", function (result) {              //                            alert("abc");              //                            $.each(result, function (i, item) {              //                                $("<option></option>")              //                                .val(item["id"])              //                                .text(item["province"])              //                                .appendTo($("#ddlProvince"));              //                            });              //                            alert("xxx");              //                            //GetCity();              //                        });              htmlobj = $.ajax({                  url: "City/GetProvince",                  async: false,                  complete: function (XmlHttpRequest, textStatus) {                  },                  success: function (result) {                      $.each(result, function (i, item) {                          $("<option></option>")                                              .val(item["provinceID"])                                              .text(item["province"])                                              .appendTo($("#ddlProvince"));                      });                      GetCity();                  },                  error: function (XmlHttpRequest, textStatus, errorThrown) {                      var $errorPage = XmlHttpRequest.responseText;                      alert($("#ErrorMsg", $errorPage).text());                      //alert($("#ErrorMsg", XmlHttpRequest.responseText).text());                  },                  dataType: "json"              });          }            function GetCity() {              $("#ddlCity").empty();               alert($("#ddlProvince").val());              htmlobj = $.ajax({                  url: "City/GetCity/" + $("#ddlProvince").val(),                  async: false,                  complete: function (XmlHttpRequest, textStatus) {                  },                  success: function (result) {                      $.each(result, function (i, item) {                          $("<option></option>")                                              .val(item["cityID"])                                              .text(item["city"])                                              .appendTo($("#ddlCity"));                      });                      GetArea();                  },                  error: function (XmlHttpRequest, textStatus, errorThrown) {                      var $errorPage = XmlHttpRequest.responseText;                      alert($("#ErrorMsg", $errorPage).text());                      //alert($("#ErrorMsg", XmlHttpRequest.responseText).text());                  },                  dataType: "json"              });          }            function GetArea() {              $("#ddlArea").empty();               alert($("#ddlCity").val());              htmlobj = $.ajax({                  url: "City/GetArea/" + $("#ddlCity").val(),                  async: false,                  complete: function (XmlHttpRequest, textStatus) {                  },                  success: function (result) {                      $.each(result, function (i, item) {                          $("<option></option>")                                              .val(item["areaID"])                                              .text(item["area"])                                              .appendTo($("#ddlArea"));                      });                  },                  error: function (XmlHttpRequest, textStatus, errorThrown) {                      var $errorPage = XmlHttpRequest.responseText;                      alert($("#ErrorMsg", $errorPage).text());                      //alert($("#ErrorMsg", XmlHttpRequest.responseText).text());                  },                  dataType: "json"              });          }                    </script>      <input id="getP" name="getP" type="button" value="获取列表" />      <h2>          xxx</h2>      <table>          <tr>              <td>                  <select id="ddlProvince"  />              </td>              <td>                  <select id="ddlCity" />              </td>              <td>                  <select id="ddlArea" />              </td>          </tr>      </table>  </asp:Content>

最后的效果:

image

代码下载:MvcApplication520110801.rar

.thick {font-weight: bold} #thick1 {font-weight: bold}
职业指导:
在使用某一技能三个月后,你还不是专家,即便使用时间是三年,你还不是。马尔科姆·格莱德威尔在《异类》一书中指出,成为一名真正的专家,需要10000小时。10000小时!如果一天用10小时,每天都学习,则大概需要3年时间。如果一天5小时,一年学习200天,则大概需要10年时间。10年!
联系我们CONTACT 扫一扫
愿景:成为最专业的软件研发服务领航者
中睿信息技术有限公司 广州•深圳 Tel:020-38931912 务实 Pragmatic
广州:广州市天河区翰景路1号金星大厦18层中睿信息 Fax:020-38931912 专业 Professional
深圳:深圳市福田区车公庙有色金属大厦509~510 Tel:0755-25855012 诚信 Integrity
所有权声明:PMI, PMP, Project Management Professional, PMI-ACP, PMI-PBA和PMBOK是项目管理协会(Project Management Institute, Inc.)的注册标志。
版权所有:广州中睿信息技术有限公司 粤ICP备13082838号-2