. NET MVC DropDownList drop-down box _ Infinite class classification

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.