申明:某些没素质的人转载了这篇文章都没留下作者的信息或来源,很是气愤。虽然首次发帖,也不用这样吧。
转帖请注明地址:
本人首次发帖,希望高人指点。
一.先看看表:
看看效果:
二.视图层:
-------------------------------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);
} }}转帖请注明地址: