mvc 三级联动

31 篇文章 0 订阅
25 篇文章 0 订阅

一.先看看表:

看看效果

二.视图层:

-------------------------------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;

转帖请注明地址:http://www.cnblogs.com/yzyeilin/archive/2011/10/18/eilin_yong.html

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);
        } 
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值