how assign json data to JIT spacetree from ActionResult mvc

396 views Asked by At

I am building a JIT spacetree like below: http://philogb.github.io/jit/static/v20/Jit/Examples/Spacetree/example1.html

I need to build dynamic Json for it using data coming from database table in MVC.

Currently I called a ActionResult as follows:

 var url = "@Url.Action("BinaryTreeData", "Dashboard")";
$.post(url, {}, function (data, status) {
            var json1 = $.parseJSON(data.html);
            alert(json1.bar);
        });

How can I build dynamic Json in ActionResult and can assign it to tree?

2

There are 2 answers

0
MichaelElfial On

Use jquery ajax method and specify json as type for the expected answer. On the server side you can return JsonResult or use the javascript serializer in more generic manner, possibly even defining your own action result (which may be useful depending on what else you will need to do). The serializers in .NET are capable of serializing various object grafs through different types, but this all depends on the structure of the extracted data from database (I cannot guess wxactly what you have there). Still, I think you can start this road and find most answers in the documentation, but be careful - the .NET JSON serializers have some specifics in the gray JSON areas (like datetime formats).

0
ankitr On

yes you can do it like I did. I did the same:

call initialiseData() instead init();

function initialiseData(){
    url = //your file path
    $.ajax({
        url: url,
        type: 'GET',
        datatype: "html",
        success: function(data) {
            init(data);
        },
        error: function(e) {
            console.log(e.message);
        }
    });
    }

Change your init() function to this.

init(json){
  ...
}

It will work.

Note: You have to have exact json format as given in example.