ASP. NET MVC object and collection serialization

Source: Internet
Author: User

Private list<model.organization.organization> getorganiztions ()
{
var organizations = new list<model.organization.organization> ();

Organizations. ADD (New Model.Organization.Organization
{
Id = "1",
Name = "FAW",
});

Organizations. ADD (New Model.Organization.Organization
{
Id = "2",
Name = "Red Flag",
ParentID = "1"
});
Organizations. ADD (New Model.Organization.Organization
{
Id = "3",
Name = "Pentium",
ParentID = "1"
});
Organizations. ADD (New Model.Organization.Organization
{
Id= "4",
Name= "Pentium B70",
Parentid= "3"
});
return organizations;
}

private void Attachchildren (TreeModel tree, list<model.organization.organization> organizations)
{
if (Equals (tree, NULL))
{
Return
}

var organizationstmp = organizations. Where (p = =!string. IsNullOrEmpty (P.parentid) && p.parentid.equals (tree.id)). ToList ();
foreach (Model.Organization.Organization node in organizationstmp)
{
var t = new TreeModel () {
Id=node. Id
Text=node. Name
};
TREE.CHILDREN.ADD (t);
This. Attachchildren (T, organizations);
}
}

Public ilist<treemodel> Gettree ()
{
var organizations = this. Getorganiztions ();
var RootNode = organizations. Find (P. = String. IsNullOrEmpty (P.parentid));
var tree = new TreeModel ()
{
id = rootnode.id,
Text = Rootnode.name
};

This. Attachchildren (tree, organizations);
return new list<treemodel> () {tree};
}

If Gettree returns a collection, the Controler call Gettree is serialized as JSON after:

[{"id": "1", "Text": "Faw", "Children": [{"id": "2", "Text": "Red Flag", "Children": []},{"id": "3", "text": "Pentium", "children": [{"id ":" 4 "," Text ":" Pentium B70 "," Children ": []}]}]

If Gettree returns an object, the Controler call Gettree is serialized as JSON after:

{"id": "1", "Text": "Faw", "Children": [{"id": "2", "Text": "Red Flag", "Children": []},{"id": "3", "text": "Pentium", "children": [{"id" : "4", "Text": "Pentium B70", "Children": []}]}]}

In the front-end of ASP. We may often use JSON arrays, we can consider the use of JSON (collection) way, so that can not be processed at the front end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.