zoukankan      html  css  js  c++  java
  • MVC TIP6:级联菜单

    级联菜单最有名的是省市级联,如果你还没有这样的数据库,请从这里下载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

  • 相关阅读:
    Kubernetes 集成研发笔记
    Rust 1.44.0 发布
    Rust 1.43.0 发布
    PAT 甲级 1108 Finding Average (20分)
    PAT 甲级 1107 Social Clusters (30分)(并查集)
    PAT 甲级 1106 Lowest Price in Supply Chain (25分) (bfs)
    PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)
    PAT 甲级 1104 Sum of Number Segments (20分)(有坑,int *int 可能会溢出)
    java 多线程 26 : 线程池
    OpenCV_Python —— (4)形态学操作
  • 原文地址:https://www.cnblogs.com/luminji/p/2123960.html
Copyright © 2011-2022 走看看