Datasource, odata and asp webapi

2 posts, 0 answers
  1. pete
    pete avatar
    20 posts
    Member since:
    Nov 2012

    Posted 03 Mar 2014 Link to this post

    I've got odata to work with a webapi call to a listview and I'm struggling with what appears to be a quirk in mvc.

    If I return a pageresult from my webapi controller I just get the data as a json array i.e. no Count value.

    To overcome this tiny issue I copy the Count value to the response header so I can pick it up in the client.

    I can easily access the Count value in the transport complete event:

            var datasource = new{
                type: "odata",
                transport: {
                    read: {
                        beforeSend: function (req) {
                            //req.setRequestHeader('Accept', 'application/json;odata=verbose');
                        url: "/api/products",
                        dataType: "json",
                        complete: function (jqXHR, textStatus) {
                            count = jqXHR.getResponseHeader("Last-Count");


    But this fires after the "Total" is processed so my Count is always one event behind.

    How can I examine do something like count = jqXHR.getResponseHeader("Last-Count"); in the "Total" as I can't see where jqXHR is exposed in this part?

  2. pete
    pete avatar
    20 posts
    Member since:
    Nov 2012

    Posted 04 Mar 2014 in reply to pete Link to this post

    While I've been waiting for a reply I've modified to return the response headers. It isn't an ideal solution as I'll need to do this each update of Kendo Mobile but it has the desired result.

    navigate to e.ajax(n) and replace rhe read section accordingly:

                    read: function (n) {
                        var i, r, o, a = this,
                            s = a.cache;
                        n = a.setup(n, pt), i = n.success || st, r = n.error || st, o = s.find(, o !== t ? i(o) : (n.success = function (e, p1, p2) {
                            p3 = Object;
                            p3.rows = e;
                            p3.xhr = p2;
                            s.add(, p3), i(p3)
                        }, e.ajax(n))

    I put the data returned from the ajax call in "data.rows" and the jqXHR object in "data.xhr".

    It's a quick fix, it's not ideal but it does appear to work.

Back to Top