MVC4中EasyUI Tree异步加载JSON数据生成树

 

1,首先构造tree接受的格式化数据结构MODEL

     ///

     /// 定义EasyUI树的相关数据,方便控制器生成Json数据进行传递

     ///

     // [DataContract]

    [Serializable]

    public class EasyTreeData

    {

        ///

        /// ID

        ///

       //  [DataMember]

        public string id { get; set; }

 

        ///

        /// 节点名称

        ///

       // [DataMember]

        public string text { get; set; }

       

        ///

        /// 是否展开

        ///

        //[DataMember]

        public string state  { get; set; }

 

        ///

        /// 图标样式

        ///

       // [DataMember]

        public string iconCls { get; set; }

 

 

        ///

        /// 子节点集合

        ///

      //  [DataMember]

        public List children { get; set; }

       

        ///

        /// 默认构造函数

        ///

        public EasyTreeData()

        {

            this.children = new List();

            this.state = "open";

        }

 

        ///

        /// 常用构造函数

        ///

        public EasyTreeData(string id, string text, string iconCls = "", string state = "open")

            : this()

        {

            this.id = id;

            this.text = text;

            this.state = state;

            this.iconCls = iconCls;

        }

 

        ///

        /// 常用构造函数

        ///

        public EasyTreeData(int id, string text, string iconCls = "", string state = "open", List children=null )

            : this()

        {

            this.id = id.ToString();

            this.text = text;

            this.state = state;

            this.iconCls = iconCls;

            this.children = children;

        }

    }

2,前台代码

2.1,前台代码html代码

     

    2.2,前台代码脚本

       

    3,控制器(Cultivate)的方法GetUserTreeJson'

    3.1,控制器(Cultivate)的方法GetUserTreeJson代码

           [HttpPost]

            public ActionResult GetUserTreeJson()

            {

                int PID = -1;

                List treeList = new List();

                if (PID == -1)

                {

                    var list = (from p in db.cultivates

                                where p.NodePID == PID

                                orderby p.NodeID

                                select p).ToList();

                    foreach (cultivate info in list)

                    { var child = (from p in db.cultivates

                                   where p.NodePID == info.NodeID

                                   orderby p.NodeID

                                   select p).ToList();

                    if (child.Count ==0)

                    {

                        treeList.Add(new EasyTreeData(info.NodeID, info.NodeName, "icon-user"));

                    }

                    else

                    {

                        treeList.Add(new EasyTreeData(info.NodeID, info.NodeName, "icon-user","open", GetUserTreeJson2(info.NodeID)));

                    

                    }

                    }

                }    

                string json = ToJson(treeList);

                return Content(json);

            }

    3.2,如果有N级菜单循环代码

            private List GetUserTreeJson2( int PID)

              {

                 List treeList = new List();

                  var list = (from p in db.cultivates

                              where p.NodePID == PID

                              orderby p.NodeID

                              select p).ToList();

                  foreach (cultivate info in list)

                  {

                      var child = (from p in db.cultivates

                                   where p.NodePID == info.NodeID

                                   orderby p.NodeID

                                   select p).ToList(); ;

                      if (child.Count == 0)

                      {

                          treeList.Add(new EasyTreeData(info.NodeID, info.NodeName, "icon-user"));

                      }

                      else

                      {

                          treeList.Add(new EasyTreeData(info.NodeID, info.NodeName, "icon-user", "closed", GetUserTreeJson2(info.NodeID)));

                      }

                  }

                  return treeList;

              }

          

    3.3,Json代码生成。

           protected string ToJson(object obj)

            {

                string jsonData = (new JavaScriptSerializer()).Serialize(obj);

                return jsonData;

            }

     

    你可能感兴趣的:(MVC4中EasyUI Tree异步加载JSON数据生成树)