1

I am trying to bind data from json. In the controller, I am sending

......
public JsonResult LoadTree()
........
return Json(jn, JsonRequestBehavior.AllowGet);

In debug I get the values in jn (47 items, each has two enteries (text and value).

In the view, I am using the following script:

function onDataBinding(e) {
    var url = 'CourseCases/LoadTree';
    var result;
    $.ajax({
        url: url,
        data: {},
        contentType: "application/json",
        success: function (data) {
            alert(data);
            var treeview = $("#TreeView").data("tTreeView");
            treeview.bindTo(data);
        }
    });
}

It does not work, alert shows object, object; and the treeview is blank! Any idea why? Thanks in advance.

0

2 Answers 2

1

Actually the properties that you are sending should be called Value and Text instead of value and text. Here's an example that works fine for me.

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult LoadTree()
    {
        var jn = new[] 
        {
            new { Value = "1", Text = "Item 1" },
            new { Value = "2", Text = "Item 2" },
            new { Value = "3", Text = "Item 3" },
        };
        return Json(jn, JsonRequestBehavior.AllowGet);
    }
}

View (~/Views/Home/Index.cshtml):

<script type="text/javascript">
    function onDataBinding(e) {
        var url = '@Url.Action("LoadTree")';
        var result;
        $.ajax({
            url: url,
            data: { },
            success: function (data) {
                var treeview = $("#TreeView").data("tTreeView");
                treeview.bindTo(data);
            }
        });
    }    
</script>

@(Html
    .Telerik()
    .TreeView()
    .Name("TreeView")
    .ClientEvents(events =>
    {
        events.OnDataBinding("onDataBinding");
    })
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Darin for your assistance.
0

Probably you should call treeview.bindTo in the callback method of $.post directly. I guess with the current code you might bind the treeview to jsonObject == jn before jn == content is ensured in the callback. If you alert you add a big delay and it gives the AJAX post enough time to complete and run the callback.

1 Comment

Thanks, the problem is I see the json data when I debug the program in the controller, but when I have alert in the view it is null!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.