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

[Solved] grid wih custom editor post new record to odata

6 Answers 121 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Wit
Top achievements
Rank 1
Wit asked on 17 Sep 2014, 02:06 PM
I have a grid with custom editor. I would like to add a new record. Data would be saved to server when 'Save changes' is pressed.

service: web api with odata controller

Example without save:
http://dojo.telerik.com/oXAY

When I send a whole data object to service odata is throwing and error: 'Cannot convert a primitive value to the expected type 'Edm.Int32'. '

I came across parameterMap which I use to remove object properties associated with custom editor. 
parameterMap: function (data, type) {
       if (type == "create" ) { 
              delete data.Application;
}
return kendo.stringify(data);

What would be the correct way of posting data back to server?

6 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 19 Sep 2014, 03:13 PM
Hello Witek,

I do not seem to understand what exactly is the issue that you experience could you please elaborate on your question? Basically to send data to server you should define update/create transport configuration.

http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-transport.create

If possible send a project with that exception so we can investigate what exactly fails.

Kind Regards,
Petur Subev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Wit
Top achievements
Rank 1
answered on 22 Sep 2014, 01:53 PM
Hi Petur,

Please see included link to project .

page is located under: app/templates/entity/entity.tpl.html

Please place breakpoint in post or put and try to add new record or modify 'owned by' value on one of existing ones in the grid.
Controllers/EntitiesController.cs

Thank you
Wit
0
Accepted
Daniel
Telerik team
answered on 24 Sep 2014, 10:18 AM
Hello Wit,

The described error will be thrown because of the type of the EntityRef field. The property type is int in the entity but is declared as string in the model:
EntityRef: {
    type: "string",
    editable: false
},
so its default value will be an empty string. You should set the type to "number" in order to avoid this error. The problem on update will occur because the OwnedByRef value will not be the same as the corresponding value in the Application object. Using the parameterMap to set the same value is one possible solution. In this case you can serialize the data as in the snippet below:
parameterMap: function (data, type) {
    if (type == "create" || type == "update") {
        if (data.Application) {
            data.OwnedByRef = data.Application.ApplicationRef;                                    
        }                                   
    }
    var result = kendo.data.transports.odata.parameterMap(data, type);
    delete result.$format;
    return result;
}
An alternative would be to use the grid save event.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Wit
Top achievements
Rank 1
answered on 24 Sep 2014, 11:09 AM
That looks great, what if we remove EntityRef field form columns and model?
0
Daniel
Telerik team
answered on 26 Sep 2014, 07:35 AM
Hello again Wit,

Removing the EntityRef field from the columns will not make a difference. Removing it will from the model will however cause the same problem. Since the field is set as the model id, it will still be added to the new items and its value will default to empty string if the type is not specified.

Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Wit
Top achievements
Rank 1
answered on 26 Sep 2014, 08:56 AM
Thank you
Tags
Grid
Asked by
Wit
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Wit
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or