下拉列表联动显示(Car表) 三级联动

1、Models
namespace 下拉列表联动显示_Car表_.Models
{
    public class ProductorBF
    {
        private MyDBDataContext _context=new MyDBDataContext();
        public List<productor> Select()  //厂商全部显示
        {
            return _context.productor.ToList();
        }

    }
}

namespace 下拉列表联动显示_Car表_.Models
{
    public class BrandBF
    {
        private MyDBDataContext _context = new MyDBDataContext();
        public List<brand> Select() //品牌(系列)全部显示
        {
            return _context.brand.ToList();
        }
        public List<brand> SelectByProd(string prod) //根据汽车厂商查汽车品牌
        {

            var query = _context.brand.Where(P => P.prod_code == prod);
            if (query.Count() > 0)
            {
                return query.ToList();
            }
            else
            {
                return null;
            }
         
        }
    }
}


namespace 下拉列表联动显示_Car表_.Models
{
    public class CarBF
    {
        private MyDBDataContext _context = new MyDBDataContext();
        public List<car> Select() //汽车全部显示
        {
            return _context.car.ToList();
        }
        public List<car> SelectByBrand(string brand_code) //根据汽车品牌查具体汽车
        {
            var query = _context.car.Where(P => P.brand==brand_code);
            if (query.Count() > 0)
            {
                return query.ToList();
            }
            else
            {
                return null;
            }
        }
    }
}



2、Controllers
namespace 下拉列表联动显示_Car表_.Controllers
{
    public class HomeController : Controller
    {
       
        [HttpGet]
        public ActionResult Index()
        {
            //厂家表(强类型)
            List<productor> list =new ProductorBF().Select();
            SelectList aaa = new SelectList(list,"prod_code","prod_name","p001");

            //品牌表
            List<brand> list1 = new BrandBF().SelectByProd("p001");
            SelectList bbb1 = new SelectList(list1, "brand_code", "brand_name");
            ViewBag.bbb = bbb1;

            //汽车表
            List<car> cars = new CarBF().SelectByBrand("b001");
            SelectList ccc1 = new SelectList(cars,"code","name");
            ViewBag.ccc = ccc1;

            return View(aaa);
        }

        [HttpPost]
        public ActionResult Index(string prodcode,string brandcode,string carcode) 
        { 
             //厂家表(强类型)
            List<productor> list1 = new ProductorBF().Select();
            SelectList aaa = new SelectList(list1, "prod_code", "prod_name",prodcode);

            //品牌表
            List<brand> list2 = new BrandBF().SelectByProd(prodcode); 
            SelectList bbb1 = new SelectList(list2,"brand_code","brand_name",brandcode);
            ViewBag.bbb = bbb1;
          
            
           
            //汽车表    
            //两种情况:选厂商的时候,判断一下加载的系列在不在当前厂商下
            //1、在的话,直接根据加载出来的系列代号查
            //2、不在的话,根据新选的厂商加载出来系列的第一个查
            var b = list2.Exists(P => P.brand_code == brandcode) ? brandcode : list2[0].brand_code; //假设选的是P003,但是定位的Brand是B001,所有需要判断,如果品牌里的下拉列表里有一个代号等于传进来的brand,说明有,可以查;如果不包含,需要根据你提交的厂商查出来的品牌中的第一项来查
            List<car> list3 = new CarBF().SelectByBrand(b);
            SelectList ccc1 = new SelectList(list3,"code","name",carcode);
            ViewBag.ccc = ccc1;



            return View(aaa);
        }

    }
}

3、Views
@using 下拉列表联动显示_Car表_.Models;
@model SelectList 
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
       @using (Html.BeginForm("Index", "Home", FormMethod.Post))
       {
           //new { onchange="document.forms[0].submit();"} 文本一旦改变整个表单会跟着变
            @Html.DropDownList("prodcode", Model,new { onchange="document.forms[0].submit();"});
            @Html.DropDownList("brandcode", ViewBag.bbb as SelectList,new { onchange="document.forms[0].submit();"});
           @Html.DropDownList("carcode", ViewBag.ccc as SelectList);

       }
    </div>
</body>
</html>

效果图:

posted @ 2015-07-09 19:34  XCml  阅读(477)  评论(0编辑  收藏  举报