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

OData binding for CreateEntity

1 Answer 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chuck
Top achievements
Rank 1
Chuck asked on 19 Sep 2013, 07:53 PM
We are using the grid in a MVC app. I can get the Read, Update and Delete functions to work without issue. However every time we try to do an insert the entity being passed to the controller is null. Here is the code:

OData Controller:
protected override BroadcastEventViewModel CreateEntity(BroadcastEventViewModel entity)
        {
            var bcEvent = new Model.BroadcastEvent()
            {
                Id = entity.Id,
                Name = entity.Name,
                StartDate = entity.StartDate,
                EndDate = entity.EndDate
            };
            _repository.InsertBroadcastEvent(bcEvent);
            return entity;
        }
Index.cshtml:
var crudServiceBaseUrl = "/odata/BroadcastEvents";
        var pledgesModel = kendo.observable({
            dataSource: broadcastEventDataSource = new kendo.data.DataSource({
                type: "odata",
                transport: {
                    create: {
                        url: crudServiceBaseUrl,
                        dataType: "json",
                    },
                    read: {
                        url: crudServiceBaseUrl,
                        dataType: "json"
                    },
                    update: {
                        url: function (data) {
                            return crudServiceBaseUrl + "(" + data.Id + ")";
                        },
                        dataType: "json"
                    },
                    destroy: {
                        url: function (data) {
                            return crudServiceBaseUrl + "(" + data.Id + ")";
                        },
                        dataType: "json"
                    }
                },
                batch: false,
                serverPaging: true,
                serverSorting: true,
                serverFiltering: true,
                pageSize: 5,
                schema: {
                    data: function (data) {
                        return data.value;
                    },
                    total: function (data) {
                        return data["odata.count"];
                    },
                    errors: function (data) {
                    },
                    model: {
                        id: "Id",
                        fields: {
                            Id: { type: "number", editable: false, nullable: true },
                            Name: { type: "string" },
                            StartDate: { type: "datetime" },
                            EndDate: { type: "datetime" },
                        }
                    }
                },
                error: function (e) {
                    var response = e.xhr.responseJSON;
                    var message = response.Message;
                    //if (e.xhr.responseJSON["odata.error"].Message != null) {
                    //    message = e.xhr.responseJSON["odata.error"].Message;
                    //}
                    alert(message + "\n\n");
                }
            })
        });
 
        $("#broadcastEventGrid").kendoGrid({
            dataSource: broadcastEventDataSource,
            toolbar: ["create", "save", "cancel"],
            sortable: false,
            pageable: true,
            filterable: false,
            columns: [
               { field: "Id", title: "ID", width: 150 },
               { field: "Name", title: "Name", width: 200 },
               { field: "StartDate", title: "Start Date", width: 200 },
               { field: "EndDate", title: "End Date", width: 200 },
               {
                   command: ["edit", {
                       name: "Details",
                       click: function (e) {
                           // var tr = $(e.target).closest("tr");
                           //var data = this.dataItem(tr);
                           alert("Details for: " + e.target);
                       }
                   }], title: "Action",
               }
            ],
            editable: {
                createAt: "bottom",
                mode: "inline"
            }
        });
I have found tons of articles and examples using the ActionLinks, but nothing that would seem to tell me why this call to Create would not work.

Thanks for any help you can provide.
Chuck

1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 23 Sep 2013, 07:38 AM
Hello Chuck,

I'm afraid that from the code posted we are not able to determine what might be causing a issue. The following github project has sample implementation of CRUD operations with OData services which you can use as a reference.

If it doesn't help it will be best to send us a runnable sample where the issue can be observed. Thus we'll be able to debug it locally and assist you furher.

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
Grid
Asked by
Chuck
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or