Link to home
Start Free TrialLog in
Avatar of z488758
z488758Flag for Australia

asked on

DynaTree and Ajax Loading

Hi

ASP.NET MVC 4, I am trying to use Dynatree, loading data via an Ajax call

Simple code follows. The Ajax call is getting three records, but nothing is making it to the treeview. Any help would be appreciated


<script type="text/javascript">
    $(function () {
        $("#tree").dynatree({      
            initAjax: {              
                url: "/Maintenance/_AjaxGetActiveCategories",            
                data: { key: "root",
                    mode: "all"}
                }
            });          
            });
 

</script>
</head>

<body class="example">
  <h1>Example: Init from Ajax request</h1>
  <p class="description">
    This sample initializes the IC tree from a JSON request.
  </p>

  <!-- Add a <div> element where the tree should appear: -->
  <div id="tree">  </div>

//================== Ajax
 public ActionResult _AjaxGetActiveCategories([DataSourceRequest]DataSourceRequest request)
        {
            TestDatabaseEntities db = new TestDatabaseEntities();

            var categories = (from x in db.Categories                        
                             select x).ToList();

            DataSourceResult result = categories.ToDataSourceResult(request);

            return Json(result,JsonRequestBehavior.AllowGet);
        }
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Could you confirm /Maintenance/_AjaxGetActiveCategories return something like :

[
    {"title": "Item 1"},
    {"title": "Folder 2", "isFolder": true, "key": "folder2",
        "children": [
            {"title": "Sub-item 2.1"},
			{"title": "Sub-item 2.2"}
			]
        },
	{"title": "Folder 3", "isFolder": true, "key": "folder3",
	    "children": [
		    {"title": "Sub-item 3.1"},
		    {"title": "Sub-item 3.2"}
		    ]
		},
	{"title": "Item 5"}
]

Open in new window


?

Do you have a link to see your page?
Avatar of z488758

ASKER

Thanks for your comments / assistance.

I am relatively inexperienced, so please bear with me

(a) I don't know how to interrogate the data. I can see the RESULT in the controller, which is the data in the list. I do not know how to see it when it returns as JSON ??
(b) Not sure what the comment "do you have a link to see your page" means? I can see the page that I am drawing the treeview on which is simply a treeview with no nodes
(c) It won't be mapped as you say. I am wanting to get the data from a database, an ajax call that returns as json, I am not sure how you map it. However, I do only want the root nodes., assuming will use lazy loading to load the children when selected

Sorry if my responses are a bit "ignorant"
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of z488758

ASKER

Thanks

1. it is a development project so not published, just on development machine not publicly visible

2. Thanks for pointing to how to see data, it is as follows. I have no idea how to map to a format dynatree is happy with?

{"Data":[{"Id":1,"Name":"Motor Vehicle","SourceId":0,"EntityState":2,"EntityKey":{"EntitySetName":"Categories","EntityContainerName":"TestDatabaseEntities","EntityKeyValues":[{"Key":"Id","Value":1}],"IsTemporary":false}},{"Id":2,"Name":"Personal Indemnity","SourceId":0,"EntityState":2,"EntityKey":{"EntitySetName":"Categories","EntityContainerName":"TestDatabaseEntities","EntityKeyValues":[{"Key":"Id","Value":2}],"IsTemporary":false}},{"Id":3,"Name":"Category 3","SourceId":0,"EntityState":2,"EntityKey":{"EntitySetName":"Categories","EntityContainerName":"TestDatabaseEntities","EntityKeyValues":[{"Key":"Id","Value":3}],"IsTemporary":false}}],"Total":3,"AggregateResults":null,"Errors":null}