This method actually still uses the list collection method to pass it to the front desk, but has made some small changes in the front desk, and the controller code has also been partially optimized. It is worth mentioning that it is useless. Ajax front-end and back-end interaction is abandoned.
The controller code is as follows:
//实例化公共静态字典表集合 public static List<TC_DictionaryInfo> DInfo = new List<TC_DictionaryInfo>(); /// <summary> /// TreeView视图 /// </summary> /// <returns></returns> public ActionResult May(string TypeCode) { ViewBag.TypeCode = TypeCode; List<TC_DictionaryInfo> DInfo = dbll.GetModelList("TypeCode=" + TypeCode); List<NodeModel> list = GetChildNodes(0, new NodeModel() { }, DInfo).nodes; ViewBag.data = list; } ///<summary> /// GetChildNodes方法,此方法使用递归 /// </summary> /// <param name="parentId"></param> /// <returns></returns> public NodeModel GetChildNodes(int parentId, NodeModel childnodestr, List<TC_DictionaryInfo> DInfo) { List<TC_DictionaryInfo> DictionaryList = DInfo.Where(e => Convert.ToInt32(e.ParentId) == parentId).ToList(); for (int i = 0; i < DictionaryList.Count; i++) { NodeModel NewNode = new NodeModel(); NewNode.DicId = DictionaryList[i].DicId; NewNode.text = DictionaryList[i].DICName; NewNode.ParentId = DictionaryList[i].ParentId; childnodestr.nodes.Add(NewNode); GetChildNodes(NewNode.DicId, NewNode, DInfo); } return childnodestr; }
PS: It is no longer three methods but simplified to two methods (almost no changes).
The front-end code is as follows:
var data='@JsonConvert.SerializeObject( ViewBag.data)'.replace(/"/g,'"'); $(function() { $('#treeview4').treeview({ color: "#428bca", data: data, onNodeSelected: function(event, data) { alert(data); } });
PS: The open source json format serial number and deserialization class library under .NET is used here
https://www.ibm.com/developerworks/cn/web/wa-lo-json/,
The following introduces the two methods of json serialization and deserialization Important methods:
JsonConvert.SerializeObject(object value) serialization,
It has an overloaded method JsonConvert.SerializeObject(object value, params JsonConverter[] converters) .
JsonConvert.DeserializeObject(string value, Type type), deserialization,
It has an overloaded method JsonConvert.DeserializeObject(string value, Type type, params JsonConverter[] converters)
These two methods can implement basic serialization and deserialization requirements.
The function of replace in js is to replace specific symbols with the symbols you need.
The function of replace(/\/g,'"') here is to replace all / with "(because the page requires a json string).
In this case, our page can read the data and display it. BZ still feels this way is better.
The above is the jQuery TreeView tree control (2) based on MVC5 and Bootstrap introduced by the editor. The data supports json strings and list collections. I hope it will be helpful to you. If you have any If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!
For more jQuery TreeView tree control data supporting json strings and list collections, please pay attention to the PHP Chinese website!