Models structure
Productorbf
Public classPRODUCTORBF {PrivateMydbdatacontext Context =NewMydbdatacontext (); PublicList<productor>Select () {returnContext.Productor.ToList (); } PublicProductor Select (stringProd_code) { varquery = Context.Productor.Where (c = C.prod_code = =Prod_code); if(Query. Count () >0) { returnquery. First (); } return NULL; }
Brandbf
Public classbrandbf {PrivateMydbdatacontext Context =NewMydbdatacontext (); PublicList<brand>Select () {returnContext.Brand.ToList (); } PublicList<brand> Select (stringProd_code) { varquery = Context.Brand.Where (c = C.prod_code = =Prod_code); if(Query. Count () >0) { returnquery. ToList (); } return NULL; } Public BOOLSelectbool (stringBrandcode) { varquery = Context.Brand.Where (p=>p.brand_code==Brandcode); if(Query. Count () >0) { return true; } return false; } }
Carbf
Public classCARBF {PrivateMydbdatacontext Context =NewMydbdatacontext (); PublicList<car>Select () {returnContext.Car.ToList (); } PublicList<car> Select (stringBrand_Code) { varquery = Context.Car.Where (c=>c.brand==Brand_Code); if(Query. Count () >0) { returnquery. ToList (); } return NULL; } }
Controller
PublicActionResult Index () {List<Productor> LISTP =NewPRODUCTORBF (). Select (); //Parameters: Collection, column values, displayed columns, selected display valuesSelectList Listprod =NewSelectList (LISTP,"Prod_code","Prod_name","P001"); List<Brand> Listb =Newbrandbf (). Select (); SelectList Listbrand=NewSelectList (Listb,"Brand_Code","Brand_Name"); Viewbag.brands=Listbrand; List<Car> LISTC =NewCARBF (). Select (); SelectList Listcar=NewSelectList (LISTC,"Code","Name"); Viewbag.cars=Listcar; returnView (Listprod); } [HttpPost] PublicActionResult Index (stringProdstringBrandstringcar) { //PROD factory number, each submitted by the number is correct. Change the manufacturer to submit (change the second level of the system submitted) the PROD are all right.List<productor> LISTP =NewPRODUCTORBF (). Select (); SelectList Listprod=NewSelectList (LISTP,"Prod_code","Prod_name", prod); //according to the correct factory number PROD query the car system is also correct//By locating the car in the drop- down list, you can tell if you have changed the data submitted by the vehicle system. If there is no change in the car system, submit,//Brand is the last old data, and if you change the system submission, Brand is the newly selected data .list<brand> Listb =Newbrandbf (). Select (PROD); varc =NewBRANDBF (). Selectbool (Brand)? brand:listb[0]. Brand_Code; SelectList Listbrand=NewSelectList (Listb,"Brand_Code","Brand_Name", c); Viewbag.brands=Listbrand; //The same as above, the page does not operate the car system, the vehicle's query according to the newly detected car system number of the query//page operation changes in the case of the car system, according to the number of the submitted car system query varb = listb.exists (p = = P.brand_code = = Brand)? brand:listb[0]. Brand_Code; List<Car> LISTC =NewCARBF (). Select (b); SelectList Listcar=NewSelectList (LISTC,"Code","Name", car); Viewbag.cars=Listcar; returnView (Listprod); }
Views
< Div > @using (@Html. BeginForm ("Index", "Cars", FormMethod.Post)) { @Html. DropDownList ("prod", Model, new {onchange = "document.forms[0].submit ()"}) @*js method, the value changes triggered, the effect ==html in the first form of the ' submit ' submission *@ @Html. DropDownList ("brand", Viewbag.brands as SelectList, new { Onchange= "Document.forms[0].submit ()"}) @Html. DropDownList ("Car", Viewbag.cars as SelectList) } </div>
Effect
First level manufacturers change
Second-level car system changes
Html.dropdownlist three-level linkage