博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MVC三级下拉菜单
阅读量:5328 次
发布时间:2019-06-14

本文共 2698 字,大约阅读时间需要 8 分钟。

控制器端代码:

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
listProd = new ProductorDA().Select();//每次提交都是正确的 ViewBag.Prods = new SelectList(listProd, "Prod_Code", "Prod_Name", prod); List
listBrand = new BrandDA().SelectByProd(prod);//提交时prod提交也是正确的 ViewBag.Brands = new SelectList(listBrand, "Brand_Code", "Brand_Name",brand);//列表,Value值,Text值 var b = listBrand.Exists(p =>p.Brand_Code == brand)?brand:listBrand[0].Brand_Code; //brand提交的不一定是正确的提交,选prod时brand提交是错误的,选brand时brand提交是正确的 List
listCar = new CarDA().SelectByBrand(b); ViewBag.Cars = new SelectList(listCar, "Code", "Name",car); return View(); } [HttpGet] public ActionResult Index() { List
listProd = new ProductorDA().Select(); ViewBag.Prods = new SelectList(listProd, "Prod_Code", "Prod_Name", "p001"); List
listBrand = new BrandDA().SelectByProd("p001"); ViewBag.Brands = new SelectList(listBrand, "Brand_Code", "Brand_Name"); List
listCar = new CarDA().SelectByBrand("b001"); ViewBag.Cars = new SelectList(listCar, "Code", "Name"); return View(); } }}

视图端代码:

@using MvcApplication8.Models;@model List
@{ Layout = null;}
Index
@using(Html.BeginForm("Index","Cars", FormMethod.Post)) { @Html.DropDownList("prod", ViewBag.Prods as SelectList,new { οnchange="document.forms[0].submit();"}) @Html.DropDownList("brand",ViewBag.Brands as SelectList,new { οnchange="document.forms[0].submit();"}) @Html.DropDownList("car",ViewBag.Cars as SelectList) }

Model层方法代码:

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MvcApplication8.Models{    public class CarDA    {        private CarsDataContext _Context = new CarsDataContext();        public List
Select() { return _Context.Car.ToList(); } public List
SelectByBrand(string brandCode) { var query = _Context.Car.Where(p => p.Brand == brandCode); return query.ToList(); } }}

 

转载于:https://www.cnblogs.com/dlexia/p/4649160.html

你可能感兴趣的文章
WPF中Image显示本地图片
查看>>
Hyper-V虚拟机上安装一个图形界面的Linux系统
查看>>
js千分位处理
查看>>
字符串类型的相互转换
查看>>
基础学习:C#中float的取值范围和精度
查看>>
Vim配置Node.js开发工具
查看>>
web前端面试题2017
查看>>
ELMAH——可插拔错误日志工具
查看>>
MySQL学习笔记(四)
查看>>
【Crash Course Psychology】2. Research & Experimentation笔记
查看>>
SOPC Builder中SystemID
查看>>
关于 linux 的 limit 的设置
查看>>
HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
查看>>
vim中文帮助教程
查看>>
MySQL基础3
查看>>
RxJS & Angular
查看>>
面向对象(多异常的声明与处理)
查看>>
MTK笔记
查看>>
ERROR: duplicate key value violates unique constraint "xxx"
查看>>
激活office 365 的启动文件
查看>>