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

[Solved] Insert in DB Issue, using OData and Kendo Grid.

1 Answer 169 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Giulio
Top achievements
Rank 1
Giulio asked on 15 Sep 2014, 10:49 AM
Hello,

I'm using a Kendo Grid with a datasource using type Odata.
I have troubles creating a new row in the database from the datasource.
This is my datasource code:

var ds = new kendo.data.DataSource({
                type: "odata",
                transport: {
                    read: {
                        url: baseUrl,
                        dataType: "json"
                    },
                    update: {
                        url: function (data) {
                            return baseUrl + "('" + data.ID_Agenzia + "')";
                        }
                    },
                    create: {
                        url: baseUrl
                    },
                    destroy: {
                        url: function (data) {
                            return baseUrl + "('" + data.ID_Agenzia + "')";
                        }
                    }
                },
                schema: {
                    model: {
                        id: "ID_Agenzia",
                        fields: {
                            ID_Agenzia: { type: "string" },
                           // etc... my other fields omitted for brevity.
                        }
                    }
                },
                pageSize: 20,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            });

Then I tried a simple grid with the automatic toolbar Create (pretty standard, I think I can omit the code), using this DS.

As far as I understood, Kendo got a method "isNew" to discern between Create/Update and it checks if the ID is === to the default value.
All the examples I found googling around, were using the ID as a numeric incremental value... But in my table the ID is a String (obviously unique) that needs to be inserted by the user!!

Hoping I've explained myself well, the issue should be clear: If the user inserts the ID, the datasource won't recognize that it's a Create operation...
Otherwise if I forbit the manual ID insert, the create will work... but the row will be inserted in the DB with the default value (empty string) and this is wrong!

How can I solve this?
Thanks.

1 Answer, 1 is accepted

Sort by
0
Alexander Popov
Telerik team
answered on 17 Sep 2014, 06:15 AM
Hi Lorenzo,

I am afraid the scenario you described is not supported. Giving users the responsibility to enter a unique ID is not considered a good practice, so this task is usually handled by the server/database instead. The simplest solution would be to use a numeric ID field that is auto-incremented by the database engine. Another workaround would be to use two string fields - one hidden used as an ID (A), and one visible (B). Then you can modify the server code so it copies the value from B to A and return it back to the client-side so the ID could be updated. 

Regards,
Alexander Popov
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
Giulio
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or