简体   繁体   English

Kendo UI添加新记录

[英]Kendo UI add new record

I have created a odata v4 service using web api 2.2, I have successfully bound the service records to grid but i am unable to add the records. 我使用web api 2.2创建了一个odata v4服务,我已成功将服务记录绑定到网格但我无法添加记录。 Please note that i created a separate project for odata v4 service and kendo UI grid is in other project. 请注意,我为odata v4服务创建了一个单独的项目,而kendo UI grid则在其他项目中。 Below is the code for grid. 下面是网格的代码。

            <script>
                $(document).ready(function () {
                    $("#searchResults").kendoGrid({
                        dataSource: {
                            type: "odata-v4",
                            transport: {
                                read:
                                    "http://test.odata.northwind/odata/Customers",
                                create: {
                                    url: "http://test.odata.northwind/odata/Customers",
                                    dataType: "jsonp", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
                                    type:"post"
                                    },
                                parameterMap: function (data, type) {
                                    if (type == "create") {
                                        // send the created data items as the "models" service parameter encoded in JSON
                                        return { models: kendo.stringify(data.models) };
                                    }
                                }
                            },
                            pageSize: 20,

                            schema: {
                                data: "value",
                                model: {
                                    id: "CustomerID",/*
                                total: function (data) { return data['@@odata.count']; }*/
                                fields: {

                                    CustomerID:  { type: "string" },
                                    CompanyName:  { type: "string" },
                                    ContactName: { type: "string" },
                                    ContactTitle: { type: "string" },
                                    Country: { type: "string" }
                                }
                                }
                            }

                        },
                        columns: [{
                            field: "CustomerID",
                            title: "CustomerID",
                            filterable: {
                                cell: {
                                    showOperators: false
                                }
                            }

                        },


                          {
                              field: "ContactName",
                              title: "Contact Name",
                              filterable: {
                                  cell: {
                                      operator: "contains"
                                  }
                              },
                              editor: NameAutoComplete


                          }, {
                              field: "ContactTitle",
                              title: "Contact Title",
                              filterable: {
                                  cell: {
                                      operator: "contains"
                                  }
                              },
                              editor: ContactTitleComboBox
                          }, {
                              field: "CompanyName",
                              title: "Company Name",
                              filterable: {
                                  cell: {
                                      operator: "contains"
                                  }
                              }
                          },
                        {
                            field: "Country",
                            title: "Country",
                            filterable: {
                                cell: {
                                    operator: "contains"
                                }
                            }
                          , editor: categoryDropDownEditor
                        },

                                  { command: ["edit", "destroy"], title: "&nbsp;", width: "250px" }
                        ],
                        height: 550,
                        toolbar: ["create", "excel", "pdf"],
                        excel: {
                            fileName: "Kendo UI Grid Export.xlsx",
                            proxyURL: "http://demos.telerik.com/kendo-ui/service/export",
                            filterable: true
                        }, pdf: {
                            allPages: true,
                            fileName: "Kendo UI Grid Export.pdf",
                            proxyURL: "http://demos.telerik.com/kendo-ui/service/export"
                        },
                        scrollable: false,
                        pageable: true,
                        sortable: true,
                        groupable: true,
                        filterable: {
                            mode: "row"
                        },
                        editable: {
                            mode: "inline",
                            create: true,
                            update: true,
                            destroy: true
                        }
                    });
                });

                function categoryDropDownEditor(container, options) {
                    $('<input required data-text-field="Country" data-value-field="CustomerID" data-bind="value:' + options.field + '"/>')
                        .appendTo(container)
                        .width(100)
                        .kendoDropDownList({
                            autoBind: false,
                            dataSource: {
                                type: "odata-v4",
                                transport: {
                                    read: "http://test.odata.northwind/odata/Customers"
                                }

                            }
                        });
                }


                function NameAutoComplete(container, options) {
                    $('<input data-text-field="ContactName" data-value-field="CustomerID" data-bind="value:' + options.field + '"/>')
                        .appendTo(container)
                        .kendoAutoComplete({
                            autoBind: false,
                            dataSource: {
                                type: "odata-v4",
                                transport: {
                                    read: "http://test.odata.northwind/odata/Customers"
                                }
                            }
                        });
                }


                function ContactTitleComboBox(container, options) {
                    $('<input data-text-field="ContactTitle" data-value-field="CustomerID" data-bind="value:' + options.field + '"/>')
                        .appendTo(container)
                        .kendoComboBox({
                            autoBind: false,
                            dataSource: {
                                type: "odata-v4",
                                transport: {
                                    read: "http://test.odata.northwind/odata/Customers"
                                }
                            }
                        });
                }


  </script>

As shown Below when i press the update button, nothing happens, seems like the button doesn't even trigger 如下所示当我按下更新按钮时,没有任何反应,似乎按钮甚至没有触发

在此输入图像描述

Below is some of the json result i binded to the grid 下面是一些绑定到网格的json结果

在此输入图像描述

Here is how i am trying to get and add records to grid in webapi. 以下是我在webapi中尝试获取和添加记录到网格的方法。

public class CustomersController : ODataController
    {
        readonly Model1 _db = new Model1();

        [EnableQuery(PageSize = 20)]
        public IHttpActionResult Get()
        {
            return Ok(_db.Customers.AsQueryable());
        }

        public IHttpActionResult Get([FromODataUri] string key)
        {
            return Ok(_db.Customers.SingleOrDefault(t => t.CustomerID == key));
        }
        [System.Web.Mvc.HttpPost]
        public async Task<IHttpActionResult> Post(Customers customer)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            _db.Customers.Add(customer);
            await _db.SaveChangesAsync();
            return Created(customer);
        }
        [System.Web.Mvc.HttpDelete]
        public async Task<IHttpActionResult> Delete([FromODataUri] int key)
        {
            var customers = await _db.Customers.FindAsync(key);
            if (customers == null)
            {
                return NotFound();
            }
            _db.Customers.Remove(customers);
            await _db.SaveChangesAsync();
            return StatusCode(HttpStatusCode.NoContent);
        }
        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }

I have been scratching my head all day, seems like i am missing something, any help would be appreciated 我一整天都在挠头,看起来我错过了什么,任何帮助都会受到赞赏

UPDATED 更新 在此输入图像描述

It seems that you have not configured the grid to allow editing. 您似乎尚未配置网格以允许编辑。 Although you added the create button, you need to change the editable field to allow editing as follows: 虽然您添加了创建按钮,但您需要更改可编辑字段以允许编辑,如下所示:

editable: {
            mode: "inline",
            create: true,
            update: true,
            destroy: true
        }

Hope this helps.. 希望这可以帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM