【问题标题】:MVC4 bootstrap-treeview getting json data from databaseMVC4 bootstrap-treeview 从数据库中获取 json 数据
【发布时间】:2015-09-14 09:49:37
【问题描述】:

我正在使用Bootstrap-Treeview .我想从服务器获取如下格式的json数据。

 $("#tree").treeview({
   data: getTree(),
   levels: 6
 });


 function getTree() {

   var tree = [{
     text: "ED",
     icon: "glyphicon glyphicon-user",
     nodes: [{
       text: "CPD",
       icon: "glyphicon glyphicon-user",
       nodes: [{
         text: "Center Manager",
         icon: "glyphicon glyphicon-user",
         nodes: [{
           text: "Manager",
           icon: "glyphicon glyphicon-user",
           nodes: [{
             text: "Tech Head",
             icon: "glyphicon glyphicon-user",
             nodes: [{
               text: "Individual",
               icon: "glyphicon glyphicon-user",
             }]
           }]

         }]

       }]
     }]
   }];

   var jsonTree = JSON.stringify(tree);
   return jsonTree;
 };

控制器动作方法如下

  public ActionResult getJsonTree()
    {
        List<Role> _lstRole = _db.Roles.ToList();
        List<RoleVM> _lstViewRole = new List<RoleVM>();
        foreach(var _role in _lstRole)
        {
            if (_role.ChildID == null)
            {                   
                _lstViewRole=_db.Roles.Where(x=>x.Id==_role.Id).
                            Select(x=>new RoleVM(){
                            text=x.RoleName,
                            icon="glyphicon glyphicon-user"
                            }).ToList();

            }
            getChildNode(_role.Id, _lstViewRole);

        }
        var roles = _lstViewRole.ToArray();

        return Json(roles, JsonRequestBehavior.AllowGet);
    }

    private void getChildNode(int _roleId, List<RoleVM> _lstRole)
    {
        RoleVM _childNode = _db.Roles.Where(x => x.ChildID == _roleId).
                           Select(x => new RoleVM()
                           {
                               text = x.RoleName,
                               icon = "glyphicon glyphicon-user"
                           }).FirstOrDefault();
        _lstRole.Add(_childNode);

    }

我得到的json格式如下

[

  {

    "text": "ED",
    "icon": "glyphicon glyphicon-user",
    "node": null

  }, {

    "text": "CPD",
    "icon": "glyphicon glyphicon-user",
    "node": null

  }, {

    "text": "Centre Manager",
    "icon": "glyphicon glyphicon-user",
    "node": null

  }, {

    "text": "Manager",
    "icon": "glyphicon glyphicon-user",
    "node": null

  }, {

    "text": "Tech Head",
    "icon": "glyphicon glyphicon-user",
    "node": null

  },

  {
    "text": "Individual",
    "icon": "glyphicon glyphicon-user",
    "node": null
  },
  null

]

如何以正确的方式获取 json 格式。任何想法将不胜感激。

【问题讨论】:

    标签: json twitter-bootstrap asp.net-mvc-4 treeview


    【解决方案1】:

    试试这个:

    var roles = _db.Roles.Select(x => new { text = x.RoleName, icon = "glyphicon glyphicon-user" }).ToArray();
    return Json(roles, JsonRequestBehavior.AllowGet);
    

    但这不会显示子节点。

    您需要建立一个父子层次结构,但看不到您的数据以及角色之间的关系,只能猜测!

    我已经使用 Newtonsoft.Json 来执行序列化,你也可以! 给定以下类/代码

    public class RoleViewModel
        {
    
            public RoleViewModel()
            {
                this.ChildRoles = new List<RoleViewModel>();
            }
    
            public string text { get; set; }
    
            public string icon { get { return "glyphicon glyphicon-user"; } }
            [JsonIgnore]
            public int Id { get; set; }
            [JsonIgnore]
            public int? ParentId { get; set; }
    
            [JsonProperty("nodes")]
            public List<RoleViewModel> ChildRoles { get; set; }
        }
    //mimic db call:
    private static List<RoleViewModel> roles = new List<RoleViewModel>
                {
                    new RoleViewModel { Id = 1, ParentId = null, text = "ED" },
                    new RoleViewModel { Id = 2, ParentId = 1, text = "CPD" },
                    new RoleViewModel { Id = 3, ParentId = 2 ,text = "Center Manager" },
                    new RoleViewModel { Id = 4 , ParentId = 3, text = "Manager" },
                    new RoleViewModel { Id = 5 , ParentId = 4, text = "Tech Head" },
                    new RoleViewModel { Id = 6 , ParentId = 5, text = "Individual" }
                };
    

    对于递归:

    public static RoleViewModel ChildrenOf(RoleViewModel role)
            {
    
                foreach (RoleViewModel child in roles.Where(x => x.ParentId == role.Id))
                {
                    role.ChildRoles.Add(ChildrenOf(child));
                }
    
                return role;
            }
    

    对于转换:

    var role = ChildrenOf(roles[0]);
    var json = JsonConvert.SerializeObject(new[] { role });
    

    生产:

    [
        {
            "text": "ED",
            "icon": "glyphicon glyphicon-user",
            "nodes": [
                {
                    "text": "CPD",
                    "icon": "glyphicon glyphicon-user",
                    "nodes": [
                        {
                            "text": "Center Manager",
                            "icon": "glyphicon glyphicon-user",
                            "nodes": [
                                {
                                    "text": "Manager",
                                    "icon": "glyphicon glyphicon-user",
                                    "nodes": [
                                        {
                                            "text": "Tech Head",
                                            "icon": "glyphicon glyphicon-user",
                                            "nodes": [
                                                {
                                                    "text": "Individual",
                                                    "icon": "glyphicon glyphicon-user",
                                                    "nodes": []
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
    

    【讨论】:

    • 嗨 Ric 感谢您的回复。数据库中有 6 个项目,每个项目具有 Rank1、Rank2、..Rank6。我想先显示具有 Rank1 的项目,然后 Rank1 的子项将是 Rank2,Rank2 的子项将是 Rank3 明智地达到 Rank6
    • 您需要一种递归方式将子代添加到其父代 - 看看这里,看看您能想出什么:stackoverflow.com/questions/1504110/…
    • 感谢您的输入更新了问题。请查看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多