Ejemplo n.º 1
0
        public JsonResult GetRegionTree(int id)
        {
            var region = RegionApplication.GetRegion(id);

            if (region == null)
            {
                var rid = RegionApplication.GetDefaultRegionId();
                region = RegionApplication.GetRegion(rid);
            }


            Dictionary <string, object> map = new Dictionary <string, object>();

            //[
            //{ level:1,list:[{id,name,shortname}]},
            //{ level:2,list:[{id,name,shortname}]},
            //{ level:3,list:[{id,name,shortname}]},
            //]

            //添加子集
            if (region.Sub != null)
            {
                map.Add(
                    ((int)region.Level + 1).ToString(),
                    region.Sub.Select(p => new
                {
                    Id        = p.Id,
                    Name      = p.Name,
                    ShortName = p.ShortName,
                    option    = "",
                }).ToList());
            }
            var parent = 0;

            do
            {
                parent = region.ParentId; //上级节点
                var cur     = region.Id;  //当前节点
                var level   = (int)region.Level;
                var regions = RegionApplication.GetSubRegion(parent);
                var list    = regions.Select(p => new
                {
                    Id        = p.Id,
                    Name      = p.Name,
                    ShortName = p.ShortName,
                    option    = p.Id == cur ? "true" : ""
                }).ToList();
                map.Add(level.ToString(), list);
                region = region.Parent;
            } while (parent > 0);
            return(Json(map, JsonRequestBehavior.AllowGet));
        }