0

My question is a bit similar to this:

Binding Kendo Data Source with Async $.ajax calling from C# MVC Controller Action

Here's the javascript for creating the Kendo grid:

 $(document).ready(function () {
            $("#grid").kendoGrid({
                dataSource: getData(),
                height: 550,
                groupable: true,
                sortable: true,
                pageable: {
                    refresh: true,
                    pageSizes: true,
                    buttonCount: 10
                },
                columns: [{
                    field: "a",
                    title: "a",
                    width: 200
                }, {
                    field: "a",
                    title: "a"
                }]
            });
        });

Here's the getData function which I used to get data, it's an ajax callback function which calls an action in the controller called doSearch

 function getData() {
            jQuery.ajax({
                type: "POST",
                url: "@Url.Action("doSearch")",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ needSearch: true }),
                success: function (data) {
                    var dataSource = new kendo.data.DataSource({
                        data: JSON.parse(data)
                    });
                    dataSource.read();
                    return dataSource;
                },
                failure: function (errMsg) {
                    alert(errMsg);
                }
            });
        }

The callback is fine, and the 'data' has value, but the problem is: there is no data shown in the grid.

Here's the returned value for data:

 [{"a": "First record"},{"a": "Second record"}]

What's wrong with the code? Why isn't the data showing?

=================================================================

Here's the updated code for getData function:

function getData() {
 var grid = $("#grid").data("kendoGrid");
 grid.dataSource.transport.read.url = '@Url.Action("doSearch",new {needSearch = true})';
 grid.dataSource.read();
}

And here's the action:

 public ActionResult doSearch(bool needSearch)
        {
            return Json("[{\"a\": \"First record\"},{\"a\": \"Second record\"}]");
        }

But the doSearch Action in the Controller isn't firing

Community
  • 1
  • 1
User2012384
  • 4,769
  • 16
  • 70
  • 106
  • Look like `data` is already JSON so you don't need to parse it again with `JSON.parse(data)`. Try just `data: data` –  Apr 15 '15 at 08:59
  • return only data instead of datasource – Abbas Galiyakotwala Apr 15 '15 at 09:00
  • @AbbasGaliyakot Stephen Muecke I tried returning only the data and remove the JSON.parse but the result is still the same, I got no error message for that... – User2012384 Apr 15 '15 at 09:27
  • 1
    Why would you do `return Json("[{\"a\": \"First record\"},{\"a\": \"Second record\"}]");`? It should be `return Json(new[] { new { a = "First record" }, new { a = "Second record" }};` –  Apr 15 '15 at 10:07
  • @StephenMuecke Oh.. That was the problem, I can bind the data with your code, if you want, please post as answer so I can accept.. – User2012384 Apr 15 '15 at 10:10
  • 1
    @Mr.香港人, Update Mike Debela's answer with it and then accept that one :) –  Apr 15 '15 at 10:17

2 Answers2

2

I tried your way, but this works for me:

function getData() {
    var grid = $("#grid").data("kendoGrid");
    grid.dataSource.transport.options.read.url = '@Url.Action("doSearch", new {needSearch = true})';
    grid.dataSource.read();
}

alternate:

$("#grid").kendoGrid({
    dataSource: {
        type: "json",
        transport: {
            read: '@Url.Action("doSearch", new {needSearch = true})'
        }
    }
);

Note: Based on your edit, your controller method needs to be changed to

public ActionResult doSearch(bool needSearch)
{
    return Json(new[] { new { a = "First record" }, new { a = "Second record" }};
}
Mike Debela
  • 1,930
  • 3
  • 18
  • 32
1

check this

function getData() {
            jQuery.ajax({
                type: "POST",
                url: "@Url.Action("doSearch")",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ needSearch: true }),
                success: function (data) {
                    return data;
                },
                failure: function (errMsg) {
                    alert(errMsg);
                }
            });
        }
Abbas Galiyakotwala
  • 2,949
  • 4
  • 19
  • 34