json - Kendo UI Grid Inserts/Updates create Duplicate Records (again) -
i have same problem daniel had in topic, solution doesn't work me:
so use-case. users adds 2 new records 1 after another:
- presses "add new record" button of grid
- fills fields (name="alex", amount=10, comment="first").
- record 1 ready. press 'save'. (data goes controller , database)
user see 1 record in grid
press "add new record" button again
- fills fields (name="bob", amount=20, comment = "second").
record 1 ready. press 'save'. data goes controller , database. in moment happens , grid send ajax request again record 1 data controller.
user updates grid , see 3 records
"alex | 10 | first" (duplicated record) id = 1
"bob | 20 | second" id = 2
"alex | 10 | first" id = 1
they recommend return id correct binding\update of data source new record. , return (new id database comes in response bouns entity)! , doesn't help. if add first record , refresh page f5 , after add second record ok. if add one, third records - problems appears again
code in controller:
[httppost] public jsonresult create(bonusdto bonusdto) { bonusaggregate bonus; if (bonusdto.amount <= 0) throw new argumentoutofrangeexception("amount should more 0"); if (bonusdto.employeeid <= 0) throw new argumentnullexception("you should specify existing employee"); using (var dbcontext = new databasecontext()) { bonusesrepository = new bonusesrepository(dbcontext); var employeerepository = new employeesrepository(dbcontext); bonus = new bonusfactory(employeerepository).create(bonusdto); bonusesrepository.save(bonus); } httpcontext.response.statuscode = (int)httpstatuscode.created; return json(bonus); // try return id after bonus saved }
ui code
// creates bonuses grid control $("#bonusesgrid").kendogrid({ datasource: bonusesdatasource, toolbar: ["create"], editable: "inline", columns: [ "bonusid", "employeeid", { field: "employeelastname", editor: employeeautocompletingeditor, template: "#=employeelastname#" }, "amount", { field: "comment", titel: "comment", editor: textareaeditor, filterable: { operators: { number: { contains: "contains" } } } }, { command: ["edit"], title: " " } ], save: function(e) { if (newvalueemployeeid !== undefined && newvalueemployeelastname !== undefined && newvalueemployeelastname !== "") { setnewvalueemployeeidandlastname(newvalueemployeeid, newvalueemployeelastname); griddatasource.model.employeeid = newvalueemployeeid; // it's hack bind model , autocomplete control griddatasource.model.employeelastname = newvalueemployeelastname; } else { griddatasource.model.employeeid = currentvalueemployeeid; griddatasource.model.employeelastname = currentvalueemployeelastname; } }, edit: function(e) { setcurrentvalueemployeeidandlastname(e.model.employeeid, e.model.employeelastname); }, cancel: function(e) { setcurrentvalueemployeeidandlastname(e.model.employeeid, e.model.employeelastname); } });
bonus data source:
// bind json result /bonuses/getpagedjsonbonuses var bonusesdatasource = new kendo.data.datasource({ transport: { read: { url: "@url.action("getpagedjsonbonuses", "bonuses")", type : "get", contenttype: "application/json", datatype: "json", cache: false }, create: { url: "@url.action("create", "bonuses")", datatype: "json", type: "post" }, parametermap: function(options, operation) { if (operation === "update" || operation === "create") { // correct format conversion var d = new date(options.date); options.date = kendo.tostring(d, dateformat); // updates bonusdto.employeeid selected value if (newvalueemployeeid !== undefined) options.employeeid = newvalueemployeeid; } if(operation === "read") { options.filter = setformattedfilterdate(options.filter); } return options; } }, pagesize: 15, serverpaging: true, serversorting: true, serverfiltering: true, error: showerrormessage, schema: { data: "data", // pagedresponse.data total: "totalcount", // pagedresponse.totalcount model: { id: "bonusid", // data fields: { employeeid: { type: "number" }, employeelastname: { type: "string", editable: true, nulable: false, validation: { required: {message: "employee's last name required"}} }, date: { type: "date", editable: true, nullable: false, validation: { required: { message: "date required set" } } }, amount: { type: "number", editable: true, nullable: false, defaultvalue: 1, validation: { required: { message: "amount required set" } } }, comment: { type: "string", editable: true } } // fields } // model }// schema });
i haven't seen problem in code. have "complete" event handler on create , update events refreshed grid - may you:
datasource: { type: "jsonp", transport: { read: urlbase + "getall", update: { url: urlbase + "update", datatype: "jsonp", complete: function (e) { $("#grid").data("kendogrid").datasource.read(); } }, create: { url: urlbase + "create", datatype: "jsonp", complete: function (e) { $("#grid").data("kendogrid").datasource.read(); } }, destroy: { url: urlbase + "destroy", datatype: "jsonp", complete: function (e) { $("#grid").data("kendogrid").datasource.read(); } } }, ...
Comments
Post a Comment