0, the data table structure, the main attributes are: ID, parentid (parent section ID), Text, Url ... Wait a minute.
1. Create a new tree structure menumodels
1 Public classMenumodels2 {3 Private int_id;4 Private string_text;5 Private int?_parentid;6 Private string_icon;7 Private string_url;8 Private Object_menus;9 Privatedictionary<string,string> _attributes =Newdictionary<string,string>();Ten One Public intID A { - Get{return_id;} - Set{_id =value;} the } - Public int?ParentID - { - Get{return_parentid;} + Set{_parentid =value;} - } + Public stringtext A { at Get{return_text;} - Set{_text =value;} - } - Public stringicon - { - Get{return_icon;} in Set{_icon =value;} - } to Public stringURL + { - Get{return_url;} the Set{_url =value;} * } $ Publicdictionary<string,string>attributesPanax Notoginseng { - Get{return_attributes;} the Set{_attributes =value;} + } A Public ObjectMenus the { + Get{return_menus;} - Set{_menus =value;} $ } $}
View Code
2. Create the following method in the controller:
1 /// <summary>2 ///Load Tree3 /// </summary>4 /// <returns></returns>5 Publicjsonresult loadtree ()6 {7list<menumodels> list =Inittree ();8 returnJson (list,"text/html", jsonrequestbehavior.allowget);9}
View Code
1 /// <summary>2 ///Initialize tree By default find top-level menu3 /// </summary>4 /// <returns></returns>5 PublicList<menumodels>Inittree ()6 {7 varTreelist =_db. Cent_tree.tolist ();8List<menumodels> RootNode =NewList<menumodels>();9 foreach(varPlistinchTreelist.where (t = T.parentid = =NULL))Ten { OneMenumodels JT =Newmenumodels (); AJt.id =plist.id; -Jt.text =plist. Name; -Jt.parentid =plist. ParentID; theJt.icon =""; -Jt.url =plist. Systemurl; -Jt.attributes =CreateURL (Treelist, JT); -Jt.menus =Createchildtree (Treelist, JT); + Rootnode.add (JT); - } + returnRootNode; A } at /// <summary> - ///recursive spanning subtree - /// </summary> - /// <param name= "treelist" ></param> - /// <param name= "JT" ></param> - /// <returns></returns> in PrivateList<menumodels> Createchildtree (list<cent_tree>treelist, Menumodels JT) - { to intkeyID = jt.id;//root Node ID +List<menumodels> nodeList =NewList<menumodels>(); - varChildren = treelist.where (t = = T.parentid = =keyID); the foreach(varChlinchchildren) * { $Menumodels node =Newmenumodels ();Panax NotoginsengNode.id =chl.id; -Node.text =CHL. Name; theNode.parentid =CHL. ParentID; +Node.icon =""; ANode.url =CHL. Systemurl; theNode.attributes =CreateURL (treelist, node); +Node.menus =Createchildtree (treelist, node); - nodelist.add (node); $ } $ returnnodeList; - } - /// <summary> the ///Add the URL attribute to attribute, or add it here if you need another attribute - /// </summary>Wuyi /// <param name= "treelist" ></param> the /// <param name= "JT" ></param> - /// <returns></returns> Wu Privatedictionary<string,string> CreateURL (list<cent_tree>treelist, Menumodels JT) - { Aboutdictionary<string,string> dic =Newdictionary<string,string>(); $ intkeyID =jt.id; - varUrllist = treelist.where (t = = T.id = =keyID). Singleordefault (); - stringSysurl =Urllist.systemurl; - stringIndexurl =Urllist.indexurl; ADic. ADD ("Sysurl", Sysurl); +Dic. ADD ("Indurl", Indexurl); theDic. ADD ("Close","true"); - returndic; $}
View Code
3, operation, Http://localhost/Home/loadtree
ASP. NET Mvc+ef recursive spanning tree structure return JSON