Search code examples
filterkendo-uidatasourceodata

Kendo datasource is sending wrong data type in OData filter


I have a Kendo datasource defined to perform server filtering. The problem is the filter it is generating looks like this:

$filter=StatusId eq '1'

which is generating the following error:

{
    "error": {
        "code": "", 
        "message": {
            "lang": "en-US", "value": "Operator 'eq' incompatible with operand types 'System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' and 'System.String' at position 9."
        }
    }
}

The message generated is in fact incorrect. The problem is with the '1' in the filter treating the number as a string. If I manually send the request as:

$filter=StatusId eq 1 I receive the expected results back. If I manualle send the first filter, I receive the error as well.

The data source for my filter UI is defined as:

type: "odata",
transport: {
    read:{ 
        url: plm.baseUrl + "_vti_bin/ListData.svc/NpiProcessSteps?$select=Title,Id", 
        dataType: "json" 
    }
},
schema:{
    model: {
        id: "Id",
        fields:{
            Id: {
                type: "number",
                from: "Id"
            },
            Title: {
                type: "string",
                from: "Title"
            },
            __metadata: {
                type: "object",
                from: "__metadata"
            }
        }
    }
}

The column in question is defined as:

{ 
    "field": "StatusId", 
    "title": "Status",
    "template": kendo.template("${Status.Title}"),
    "width": "300px",
    "filterable": {
        "ui": plmApp.NpiStatusFilter,
        extra: false,
        operators: {
            number: {
                eq: "Is equal to",
                neq: "Is not equal to"
            }
        }
    }
}

Have I done anything wrong to have the filter sent in this way, or is these any way to ensure that the filter is sent as a number (1) rather than as a string ('1')?


Solution

  • You need to specify the type of the StatusId field as well:

       fields:{
            Id: {
                type: "number",
                from: "Id"
            },
            StatusId: {
                type: "number"
            },
            Title: {
                type: "string",
                from: "Title"
            },
            __metadata: {
                type: "object",
                from: "__metadata"
            }
        }
    

    In addition you don't need to specify from if it is the same field.