S
S
SMARTi2014-11-28 14:02:41
ASP.NET
SMARTi, 2014-11-28 14:02:41

Why are changes not saved in kendoGrid using custom editor?

Created a simple object model with products and categories:

public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual Category Category { get; set; }
    }
    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

Using KendoUI, I made a grid for displaying and editing products:
$("#productGrid").kendoGrid({
        dataSource: {
            type: "odata",
            transport: {
                read: {
                    url: "/odata/Product",
                    dataType: "json",
                    data: {
                        $select: "Id, Name, Category/Name",
                        $expand: "Category"
                    }
                },
                create: {
                    url: "/odata/Product",
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json"
                },
                update: {
                    url: function (data) {
                        return "/odata/Product(" + data.Id + ")";
                    },
                    type: "PUT",
                    dataType: "json",
                    contentType: "application/json"
                },
                destroy: {
                    url: function (data) {
                        return "/odata/Product(" + data.Id + ")";
                    },
                    type: "DELETE",
                    dataType: "json"
                },
                parameterMap: function (options, operation) {
                    var paramMap = kendo.data.transports.odata.parameterMap(options);

                    delete paramMap.$format;

                    if (operation !== "read" && options) {
                        delete paramMap.$inlinecount;
                        return JSON.stringify(paramMap);
                    }

                    return paramMap;
                },
            },
            schema: {
                data: function (data) {
                    return data.value;
                },
                total: function (data) {
                    return data['odata.count'];
                },
                model: {
                    id: "Id",
                    fields: {
                        Id: { type: "number", editable: false },
                        Name: { type: "string" },
                        Category: { defaultValue: { Name: "DefCategory" } },
                    }
                }
            },
            pageSize: 10,
            serverPaging: true,
            serverFiltering: true,
            serverSorting: true,
            serverGrouping: true
        },
        height: 550,
        sortable: true,
        editable: "inline",
        toolbar: ["create"],
        pageable: {
            refresh: true,
            pageSizes: true,
            buttonCount: 5,
        },
        columns: [
            { field: "Name", title: "Name", width: 200 },
            { field: "Category", title: "Categ", template: "#=Category.Name#", editor: categoryEditor },
            { command: ["edit", "destroy"], title: " " },
        ]
    });

To edit the category, I made a custom editor:
function categoryEditor(container, options) {
        $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                dataTextField: "Name",
                dataValueField: "Id",
                autoBind: false,
                dataSource: {
                    type: "odata",
                    schema: {
                        data: "value",
                        total: "['odata.count']",
                        model: {
                            id: "Id",
                            fields: {
                                Id: { editable: false, type: "number" },
                                Name: { type: "string", nullable: false }
                            }
                        }
                    },
                    transport: {
                        read: {
                            url: "/odata/Category",
                            dataType: "json",
                            contentType: "appliction/json"
                        }
                    }
                }
            });
    }

Ordinary text data is successfully changed, but the category first changes, but after updating the data, it becomes clear that the new value did not get into the database.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question