This is a migrated thread and some comments may be shown as answers.

[Solved] adding item to a data source

1 Answer 602 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Morten
Top achievements
Rank 2
Iron
Iron
Iron
Morten asked on 25 Sep 2014, 10:24 PM
In a asp.net mvc project I often need to fill in dropdown lists with countries to select from.
I usually setup a datasource and bind it to the dropdown list widget.
Sometimes I need to insert a "Any country" value at the top of the list.

var countryDs = new kendo.data.DataSource({
    transport: {
        read: {
            type: "POST",
            url: '@Url.Action("GetCustomerCountries", "System")',
            data: function () {
                return {
                    customerId: parseInt("@Model.CustomerId", 10)
                }
            },
            dataType: "json",
            contentType: 'application/json; charset=utf-8'
        },
        parameterMap: function (data, type) {
            return kendo.stringify(data);
        }
    },
    schema: {
        model: {
            fields: {
                CustomerId: { type: "number" },
                CountryId: { type: "number" },
                CountryName: { type: "string" },
                CountryAbbrev: { type: "string" }
            }
        }
    }
});

$("#grid-filter-country").kendoDropDownList({
    dataTextField: "CountryName",
    dataValueField: "CountryId",
    dataSource: countryDs,
    value: GetCountry()
});

This works fine, however when I try to insert the "any country" item (right after the data source definition) I get an error:

countryDs.insert(0, { "CustomerId": 1, "CountryId": 0, "CountryName": "Any country", "CountryAbbrev": "Any" });
countryDs.sync();

The error: "TypeError: r is undefined".

Can anyone point out what I am missing (I'm open for better/other solutions).

1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 29 Sep 2014, 08:20 AM
Hello Morten,

We are not sure what is causing this error. However when add record and sync the DataSource you must have definition of the create action in the transport which to handle the new records.

On the other side do you really need to add new item in the data source? You can use optionLabel instead.

Regards,
Nikolay Rusev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
Morten
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Nikolay Rusev
Telerik team
Share this question
or