Kendo UI Data Source Identifier

When I create a new server-side item using the Kendo UI data source , how can I update the client-side data item ID with the ID of the new record inserted into the server-side database?

+4
source share
4 answers

Performing more research, I found this extremely useful information, which, indeed, should be in the documents, but it is "hidden" in the search message forum, which is not very convenient for searching:

http://www.kendoui.com/forums/ui/grid/refresh-grid-after-datasource-sync.aspx#2124402

I'm not sure if this is the best approach, but it solved my problem!

This solution simply uses the read method of the data source to update the model instances with data from the server.

Valuable information where it is executed: in the complete event of the transport.create object!

Here is the code:

transport: { read: { url: "http://myurl.json" }, create: { url: "http://mycreate.json", type: "POST", complete: function(e) { $("#grid").data("kendoGrid").dataSource.read(); } }, 
+4
source

To avoid the extra server call introduced by the read method, if your create method returns an object, the data source automatically inserts it for you.

Knowing that all you need to do is set the identifier field from the database and return the model.

eg. psudo for ASP MVC action to create.

 public JsonResult CreateNewRow(RowModel rowModel) { // rowModel.id will be defaulted to 0 // save row to server and get new id back var newId = SaveRowToServer(rowModel); // set new id to model rowModel.id = newId; return Json(rowModel); } 
+1
source

I had the same problem and I think I found the answer. If in the schema you define an object that contains the results, you must return the result of the created link in the same object. Example:

 schema: { data: "Results", total: "ResultsCount", .... } 

Example MVC method:

 public JsonResult CreateNewRow(RowModel rowModel) { // rowModel.id will be defaulted to 0 // save row to server and get new id back var newId = SaveRowToServer(rowModel); // set new id to model rowModel.id = newId; return Json(new {Results = new[] {rowModel}}); } 
0
source

Just add an answer to Jack (I don't have a reputation for comment) if your Create and Update actions return data with the same schema as in the kendo data source, DataSource will automatically update the Id field like any other fields that can be changed by an action call . You do not need to do anything else that correctly shapes your results. I use this function to calculate a bunch of things on the server side and present the results to the client without the need for a complete data reload.

0
source

All Articles