How to get the total count out of JSON oData using the Kendo Grid Html Helper.

2 posts, 1 answers
  1. Rob
    Rob avatar
    3 posts
    Member since:
    Sep 2015

    Posted 11 Sep 2015 Link to this post

    Hi,

    I am having problems getting the total count field from JSON odata using the Html Helper for the Kendo Grid. Getting the odata.count value using JavaScript is fine though but our preference is to use the MVC html helper. The following is both examples:

     

    Using MVC Html Helper produces the following JavaScript error: Uncaught TypeError: Cannot read property 'count' of undefined. See the Schema property for where we are trying to get the oData.count.

    @(Html.Kendo().Grid<DataObject>()
        .Name("grid")
        .Columns(columns =>
        {
            columns.Bound(e => e.Field1).Filterable(false);
            columns.Bound(e => e.Field2);
            columns.Bound(e => e.Field3);
        })
        .DataSource(dataSource => dataSource
            .Custom()
            .Type("odata")
            .Schema(schema =>
            {
                schema.Data("value")
                     .Total("odata.count");
            })
            .Transport(transport =>
            {
                transport.Read(read => read.Url("/odata/DataUrl).DataType("json"));
            }
            )
            .PageSize(5)
            .ServerPaging(true)
            .ServerSorting(true)
            .ServerFiltering(true)
        )
        .Pageable()
        .Sortable()
        .Filterable()
    )


    Using the following JavaScript works:

    $(".grid").kendoGrid({
                dataSource: {
                    type: "odata",
                    transport: {
                        read: {
                            url: "/odata/DataUrl",
                            dataType: "json"
                        }
                    },
                    schema: {
                        data: function(data) {
    return data['value'];
                        },
                        total: function(data) {
                            return data['odata.count'];
                        },
                        model: {
                            fields: {
                                Field1: { type: "date" },
                                Field2: { type: "string" },
                                Field3: { type: "string" },
                            }
                        }
                    },
                    pageSize: 20,
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                filterable: true,
                sortable: true,
                pageable: true,
                columns: [
                    {
                        field: "Field1",
                        filterable: false,
                        format: "{0:MMM dd, yyyy}"
                    },
                    {
                        field: "Field2",
                        filterable: false
                    },
                    {
                        field: "Field3",
                        filterable: false
                    }
                ]
            });



    JSON

    {
      "odata.metadata":"http://localhost:15649/odata/$metadata#​DataUrl","odata.count":"100","value":[
        {
          "​Field1":"2015-07-21T11:45:38.927"
          ,"​Field2":"2015-07-21T11:33:41.067"
          ,"​Field3":"2015-07-21T11:45:35.993"
        }
        ...
      ]}

     

    Looking in the documentation there are only examples of this using javascript, are there some examples using the html helpers as well somewhere? Any pointers in the right direction are welcome.

    thanks,

    Rob

  2. Answer
    Alexander Popov
    Admin
    Alexander Popov avatar
    1416 posts

    Posted 15 Sep 2015 Link to this post

    Hi Rob,

    The Custom DataSource builder allows a JavaScript function to be used for the schema.total through a method override. For example: 
    .Total(@<text>function(data) { return data['odata.count']; }</text>))

    Regards,
    Alexander Popov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top