博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mvc2 三级联动
阅读量:4621 次
发布时间:2019-06-09

本文共 3589 字,大约阅读时间需要 11 分钟。

 


申明:某些没素质的人转载了这篇文章都没留下作者的信息或来源,很是气愤。虽然首次发帖,也不用这样吧。

转帖请注明地址:

 


本人首次发帖,希望高人指点。

一.先看看表:

看看效果

二.视图层:

-------------------------------JQuery代码 

<script src="http://www.cnblogs.com/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

          <script type="text/javascript">
              $(document).ready(function () {               
                  GetByJquery();
                  $("#selectgroup").change(function () { GetCompany() });
                  $("#selectcompany").change(function () { GetDepartment() });
              });
              function GetByJquery() {
                  $.getJSON("/Home/GetGroup1", function (data) {
                      $.each(data, function (i, item) {
                          $("<option></option>").val(item["Value"]).text(item["Text"]).appendTo("#selectgroup");
                      });
                  });
               }
             
              function GetCompany() {
                  $("#selectcompany").empty();
                  var url = "/Home/GetCompany/" + $("#selectgroup").val() + "/";

                  $.getJSON(url, function (data) {

                      $.each(data, function (i, item) {

                          $("<option></option>")
                                        .val(item["Value"])
                                        .text(item["Text"])
                                        .appendTo($("#selectcompany"));
                      });
                  });
              }
              function GetDepartment() {
                  $("#selectdepartment").empty();
                  var url = "/Home/GetDepartment/" + $("#selectgroup").val() + "," + $("#selectcompany").val() + "/";
                  $.getJSON(url, function (data) {

                      $.each(data, function (i, item) {

                          $("<option></option>")
                                        .val(item["Value"])
                                        .text(item["Text"])
                                        .appendTo($("#selectdepartment"));
                      });
                  });
              }   
        </script>

----------------------------------------控件

        <table>

                <tr>
                        <td>
                                <select id="selectgroup" name="groups" style="background-color:Orange; color:Black; width:100px;">

<option></option>

                                 </ select>

                       </td>
                        <td>
                                <select id="selectcompany" name="companys" style="background-color:Orange; color:Black; width:100px;">

<option></option>

                                </ select>

                        </td>
                        <td>
                                <select id="selectdepartment" name="departments"  style="background-color:Orange; color:Black; width:100px;"> <option></option> </ select>
                        </td>
                </tr>
        </table>

----------------------------------------再看看控制器:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestListBoxMvc.Models;
using System.Collections;

转帖请注明地址:

namespace TestListBoxMvc.Controllers

{
    [HandleError]
    public class HomeController : Controller
    {
         testDBEFntities test = new testDBEFntities();
        public ActionResult Index()
        {

            ViewData["Message"] ="uuuuuuuuuuuuuuuu";

            return View();

        }

        public ActionResult About()

        {
            return View();
        }
        public JsonResult GetGroup1()
        {
            IQueryable<group1> queryResult =test.group1;
            List<group1> lis = queryResult.ToList();
            List<SelectListItem> items = new List<SelectListItem>();
           foreach(var i in lis)
           {
               items.Add(new SelectListItem{Text=i.group1_name,Value=i.group1_id.ToString() });
           }
            return Json(items, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetCompany(string id)

        {
            int idd = Convert.ToInt32(id);
            IQueryable<company> qr = test.company.Where(p=>p.group_id==idd);
            List<company> lis = qr.ToList();
            List<SelectListItem> items = new List<SelectListItem>();
            foreach(var i in lis)
            {
                items.Add(new SelectListItem {Text=i.company_name,Value=i.company_id.ToString() });
            }
            return Json(items, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDepartment(string id)

        {

            string[] ids = id.Split(",".ToCharArray());

            string group1_id = ids[0];
            string company_id = ids[1];
            int cid = Convert.ToInt32(company_id);
            IQueryable<department> qr = test.department.Where(d=>d.company_id==cid);
            List<department> lis = qr.ToList();
            List<SelectListItem> items = new List<SelectListItem>();
            foreach(var i in lis)
            {
                items.Add(new SelectListItem{Text=i.department_name,Value=i.department_id.ToString()});
            }

            return Json(items, JsonRequestBehavior.AllowGet);

        }
    }
}


转帖请注明地址:

 


 

 

转载于:https://www.cnblogs.com/yzyeilin/archive/2011/10/18/eilin_yong.html

你可能感兴趣的文章
转:Java并发集合
查看>>
Word截图PNG,并压缩图片大小
查看>>
Python项目对接CAS方案
查看>>
mysql产生随机数
查看>>
编程风格
查看>>
熟悉常用的Linux命令
查看>>
易之 - 我是个大师(2014年3月6日)
查看>>
Delphi中窗体的事件
查看>>
file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did
查看>>
linux vi编辑器
查看>>
js树形结构-----(BST)二叉树增删查
查看>>
contract
查看>>
Python语言编程
查看>>
[poj 1469]Courses
查看>>
vue+element-ui实现表格checkbox单选
查看>>
测试开发学习进阶教程 视频&PDF
查看>>
C#基础-连接Access与SQL Server
查看>>
autofac
查看>>
MacOS 系统终端上传文件到 linux 服务器
查看>>
Excel导出POI
查看>>