siavash bashiri
siavash bashiri

Reputation: 419

e.slice is not a function

I have a kendoDropDownList using kendo UI and jquery. I have an error like this and don't know why I get this error.

$("#drpState").kendoDropDownList({
                optionLabel: "States...",
                delay: 10,
                dataTextField: "Name",
                dataValueField: "StateId",
                dataSource: {
                    serverFiltering: true,
                    transport: {
                        read: {
                            headers: {
                                "__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val()
                            },
                            type: "Post",
                            dataType: "json",
                            url: "/Supervision/Tracking/GetStates",
                        }
                    }
                }
            }).data("kendoDropDownList");
        [HttpPost]
        public async Task<JsonResult> GetStates(DataSourceRequest request, CancellationToken cancellationToken = default)
        {
            request.Skip = 0;
            request.Take = 100;

            var states = await _stateService.GetStates(request, cancellationToken);

            return Json(states);
        }

enter image description here

the returned data is a DataSourceResult which contains Aggregates, Data and Total. Apparently, the Data has an array of objects as you see.

enter image description here

enter image description here

Upvotes: 2

Views: 1087

Answers (1)

DontVoteMeDown
DontVoteMeDown

Reputation: 21465

That happens because Kendo expects an array from the request response, and its getting an object. You need to specify to Kendo where to find that array. Use schema.data on DataSource:

dataSource: {
    serverFiltering: true,
    transport: {
        read: {
            headers: {
                "__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val()
            },
            type: "Post",
            dataType: "json",
            url: "/Supervision/Tracking/GetStates",
        }
    },
    schema: {
        data: "Data"
    }
}

Or you can do like @GaloisGirl proposed and return an array directly.

Upvotes: 3

Related Questions