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

本文介绍了一个使用ASP.NET 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值