MVC三级下拉菜单

控制器端代码:

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);//提交时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<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>

@{
    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 { ο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)
        }



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

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<Car> Select()
        {
            return _Context.Car.ToList();
        }
        public List<Car> SelectByBrand(string brandCode)
        {
            var query = _Context.Car.Where(p => p.Brand == brandCode);
            return query.ToList();
        }
    }
}

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值