MVC 程序开发对汽车种类的联动查询。厂商,系列,型号

mo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication8.Models
{
    public class BrandDA
    {
        private masterDataContext _Context = new masterDataContext();

        //查询全部系列
        public List<Brand> Select()
        {
            return _Context.Brand.ToList();
        }
        //根据厂商查询系列
        public List<Brand> SelectByProd(string prodCode)
        {
            var query = _Context.Brand.Where(p => p.Prod_Code == prodCode);
            return query.ToList();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplication8.Models
{
    public class CarDA
    {
        private masterDataContext _Context = new masterDataContext();
        public List<Car> Select()
        {
            return _Context.Car.ToList();
        }

        //通过系列查所有型号
        public List<Car> SelectByBrand(string brandCode)
        {
            var query = _Context.Car.Where(p => p.Brand == brandCode);
            return query.ToList();
        }
    }
}

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

namespace MvcApplication8.Controllers
{
    public class CarsController : Controller
    {
        //
        // GET: /Cars/

        public ActionResult Index(string prod, string brand, string car)
        {
            //查询全部厂商
            List<Productor> listProd = new ProductorDA().Select();
            //厂商下拉表的显示
            ViewBag.Prods = new SelectList(listProd, "Prod_Code", "Prod_Name", prod);

            //通过厂商查询系列
            List<Brand> listBrand = new BrandDA().SelectByProd(prod);
            //系列下拉表的显示
            ViewBag.Brands = new SelectList(listBrand, "Brand_Code", "Brand_Name",brand);


            var b = listBrand.Exists(p =>p.Brand_Code == brand)?brand:listBrand[0].Brand_Code; 

            //通过系列查询小汽车的型号
            List<Car> listCar = new CarDA().SelectByBrand(b);

            //小汽车下拉表的显示
            ViewBag.Cars = new SelectList(listCar, "Code", "Name",car);
            return View();
        }

        

        [HttpGet]
        public ActionResult Index()
        {
            List<Productor> listProd = new ProductorDA().Select();
            ViewBag.Prods = new SelectList(listProd, "Prod_Code", "Prod_Name", "p001");

            List<Brand> listBrand = new BrandDA().SelectByProd("p001");
            ViewBag.Brands = new SelectList(listBrand, "Brand_Code", "Brand_Name");

            List<Car> listCar = new CarDA().SelectByBrand("b001");
            ViewBag.Cars = new SelectList(listCar, "Code", "Name");
            return View();
        }
    }
}
@using MvcApplication8.Models;
@model List<MvcApplication8.Models.Car>
@using MvcApplication8.Controllers;

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @using(Html.BeginForm("Index","Cars", FormMethod.Post))
        {
        @Html.DropDownList("prod", ViewBag.Prods as SelectList,new { onchange="document.forms[0].submit();"})
        @Html.DropDownList("brand",ViewBag.Brands as SelectList,new { onchange="document.forms[0].submit();"})
        @Html.DropDownList("car",ViewBag.Cars as SelectList)
        }

        <form attion="CarsControllers" method="post">

        </form>


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

原文地址:https://www.cnblogs.com/275147378abc/p/4638541.html