cs0815
cs0815

Reputation: 17428

kendo ui grid not showing data

I am trying to get this basic kendo ui with expandable rows working:

<div id="grid"></div>

<script type="text/javascript">
    $(function () {
        $("#grid").kendoGrid({
            columns: [
            {
                field: "ProductId",
                title: "ProductId"
            }
        ],
            dataSource: {
                type: "json",
                transport: {
                    read: '@Url.Action("GetData1", "MockForms")'
                }
            },
            height: 450,
            sortable: true,
            pageable: true,
            detailTemplate: "<h2 style='background-color: yellow;'>Expanded!</h2>",
            detailExpand: function (e) {
                this.collapseRow(this.tbody.find(' > tr.k-master-row').not(e.masterRow));
            }
        });
    });  
</script>

The json is generated like this:

public ActionResult GetData1([DataSourceRequest] DataSourceRequest request)
        {
            var list = new List<Product>
                {
                    new Product {ProductId = 1, ProductType = "SomeType 1", Name = "Name 1", Created = DateTime.UtcNow},
                    new Product {ProductId = 1, ProductType = "SomeType 2", Name = "Name 2", Created = DateTime.UtcNow},
                    new Product {ProductId = 1, ProductType = "SomeType 3", Name = "Name 3", Created = DateTime.UtcNow}
                };

            return Json(list.AsQueryable().ToDataSourceResult(request));
        }

and seems to be send OK (according to firebug). However, nothing is bound (there are no javascript errors). Any ideas?

PS:

OnaBai's 2nd comment helped me to get this to work. I changed:

return Json(list.AsQueryable().ToDataSourceResult(request));
=>
return Json(list);

which produces this JSON:

[{"ProductId":1,"ProductType":"SomeType 1","Name":"Name 1","Created":"\/Date(1371022051570)\/"},{"ProductId":1,"ProductType":"SomeType 2","Name":"Name 2","Created":"\/Date(1371022051570)\/"},{"ProductId":1,"ProductType":"SomeType 3","Name":"Name 3","Created":"\/Date(1371022051570)\/"}]

Still I would like to use:

return Json(list.AsQueryable().ToDataSourceResult(request));

as this will eventually make paging and sorting easier. It currently produces:

{"Data":[{"ProductId":1,"ProductType":"SomeType 1","Name":"Name 1","Created":"\/Date(1371022186643)\/"},{"ProductId":1,"ProductType":"SomeType 2","Name":"Name 2","Created":"\/Date(1371022186648)\/"},{"ProductId":1,"ProductType":"SomeType 3","Name":"Name 3","Created":"\/Date(1371022186648)\/"}],"Total":3,"AggregateResults":null,"Errors":null}

I tried to use:

field: "Data.ProductId",

instead of:

field: "ProductId",

in the JavaScript code with no avail.

Upvotes: 0

Views: 4840

Answers (1)

Atanas Korchev
Atanas Korchev

Reputation: 30661

If you want to use ToDataSourceResult you should use the ASP.NET MVC wrappers. More info is available in the documentation: http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-binding

Upvotes: 1

Related Questions