生成类别树结构json数据?

时间:2013-02-13 09:06:08

标签: jquery json asp.net-mvc-3 entity-framework

类别表:

ID => int,主键

CategoryName => VARCHAR

ParentCategoryID => int,nullable

生成以下json数据格式:

[{
    "id":1,
    "name":"Root",
    "Edit":"<a href='Edit/id'>edit.png</a>",
    "Delete":"<a href='Delete/id'>delete.png</a>",
    "children":[{
        "id":2,
        "name":"Horoscope",
        "Edit":"<a href='Edit/id'>edit.png</a>",
        "Delete":"<a href='Delete/id'>delete.png</a>",
        "children":[{
            "id":21,
            "name":"Daily",
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>",
            "children":[{
                "id":211,
                "name":"Aries",             
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            },{
                "id":212,
                "name":"Taurus",                
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            }]
        },{
            "id":22,
            "name":"Weekly",            
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>",
            "children":[{
                "id":221,
                "name":"Gemini",                
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            },{
                "id":222,
                "name":"Aries",             
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            },{
                "id":223,
                "name":"Taurus",            
                "Edit":"<a href='Edit/id'>edit.png</a>",
                "Delete":"<a href='Delete/id'>delete.png</a>"
            }]
        }]
    },{
        "id":3,
        "name":"News",      
        "Edit":"<a href='Edit/id'>edit.png</a>",
        "Delete":"<a href='Delete/id'>delete.png</a>",
        "children":[{
            "id":31,
            "name":"Sports",            
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>"
        },{
            "id":32,
            "name":"Interantional",         
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>"
        },{
            "id":33,
            "name":"Entertaintment",            
            "Edit":"<a href='Edit/id'>edit.png</a>",
            "Delete":"<a href='Delete/id'>delete.png</a>"
        }]
    }]
}]

我有Jquery TreeGrid插件,需要上面的json格式。如何从数据库的类别表生成上面的json数据并在控制器操作中返回json数据。我使用实体框架进行模型。

2 个答案:

答案 0 :(得分:1)

我不会混合数据和UI指令开始!它使用冗余重复数据使JSON消息混乱。我认为由客户决定将数据放在何处以及如何显示数据,或者您应该单独发送这些指令(例如,作为消息的第一部分)。

也就是说,完成这项工作的最简单方法就是让你的类定义如下:

class Category
{
    public int Id { get; set; }
    public string CategoryName { get; set; }
    public int? ParentCategoryID { get; set; }
    [ForeignKey("ParentCategoryID")]
    public virtual ICollection<Category> SubCategories { get; set; }
}

当您使用Categories查询ParentCategoryID == null,启用延迟加载并序列化为JSON时,您会看到包含所有级别,因为每个Category都会发出一个查询以获取它的孩子。

请注意,Category没有ParentCategory属性,因为这可能导致JSON序列化因循环引用而失败。

对于序列化,如果您不在MVC API控制器中,则可以使用JavaScriptSerializer

顺便说一句。 最佳方法是让您的域模型无法使用传输,并使用CategoryDto个对象的结构。

答案 1 :(得分:0)

我认为你需要在你的域类中有一个名为“Children”的属性,如下所示,

Category {

public int Id{get;set;}
public string CategoryName{get;set;}
public int ParentCategoryID {get;set;}
public Category ParentCategory {get;set;}

public virtual IList<Category > Children{get;set;}
}

然后你可以使用像AutoMapper这样的映射器将这些模型映射到你的视图模型(它具有json对象所需的属性)