1. Database design:
Id:int
Name:string
Pid:int//Parent ID (the top-level menu defaults to 0)
2. Controller:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using Mvctest.models;namespace mvctest.controllers{[ValidateInput (false)] public class Homecontroller:controller { Private Testentities db = new testentities (); [Loginfilter] public ActionResult Index () {binddropdownlist (); return View (); }//Bind top-level classification private void Binddropdownlist () {list<selectlistitem> Select1 = n EW list<selectlistitem> (); var one = from C in db. Categories where c.pid = = 0 Select C; foreach (Var b in one) {string name = B.name; string id = b.id.tostring (); Select1. ADD (new SelectListItem {Text = ">" + name, Value = ID }); int sonparentpid = Int. Parse (ID); string blank = "┠"; Recursive sub-classification method Bindnode (Sonparentid, blank, Select1); } viewbag.calist = new SelectList (Select1, "Value", "Text"); }//bound sub-classification private void Bindnode (int pid, string blank, list<selectlistitem> select1) { var = db. Categories.where (c = c.pid = = pId); foreach (var c in) {string zname = blank + c.name; String zid = C.id.tostring (); Select1. ADD (new SelectListItem {Text = zname, Value = Zid}); int sonparentid = Int. Parse (Zid); String blank2 = blank + "-"; Bindnode (Sonparentid, BLANK2, Select1); } } }}
3. View:
@Html. dropdownlistfor (A = A.caid, viewbag.calist as IENUMERABLE<SELECTLISTITEM>,---Please select---, new {style = "wi dth:120px; "})
. NET MVC DropDownList drop-down box _ Infinite class classification