Controller:
1 /// <summary> 2 // tree initialization 3 /// </Summary> 4 /// <returns> </returns> 5 [httppost] 6 Public contentresult inittree () 7 {8 list <testmodel> items = BLL. gettreeformlist (); 9 action <testmodel> setchildren = NULL; 10 // encapsulate a method (recursion) to constantly read the branches of the tree 11 setchildren = parent => 12 {13 parent. children = items. where (childitem => childitem. parentguid = parent. guid ). tolist (); 14 parent. children. foreach (setchildren); 15}; 16 // initialization tree list 17 list <testmodel> treeitems = items. where (rootitem => rootitem. parentguid = ""). tolist (); 18 treeitems. foreach (setchildren); 19 20 string strjson = jsonconvert. serializeobject (treeitems, 21 formatting. indented, 22 new jsonserializersettings () 23 {24 nullvaluehandling = nullvaluehandling. ignore, 25 referenceloophandling = referenceloophandling. ignore, 26 converters = new list <jsonconverter> {New isodatetimeconverter {datetimeformat = "yyyy-mm-dd hh: mm: SS" }}27 }). replace ("children", "children"); 28 29 return content (strjson); 30}
View code
Entity:
1 public class TestModel2 {3 public string Guid { get; set; }4 public string Name { get; set; }5 public string Price { get; set; }6 public string ParentGuid { get; set; }7 8 public List<TestModel> Children { get; set; }9 }
View code
Easyui tree background data initialization (LINQ Operation)