首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kendo UI添加新记录

Kendo UI添加新记录
EN

Stack Overflow用户
提问于 2015-04-14 18:42:20
回答 1查看 3.8K关注 0票数 3

我已经使用WebAPI2.2创建了一个odata v4服务,我已经成功地将服务记录绑定到网格,但是我无法添加这些记录。请注意,我为odata v4服务创建了一个单独的项目,Kendo也在其他项目中。下面是网格的代码。

代码语言:javascript
复制
<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>

如下图所示,当我按下update按钮时,任何事情都不会发生,似乎该按钮甚至不会触发

下面是我绑定到网格的一些JSON结果

下面是我如何在webapi中获取并添加记录到网格中。

代码语言:javascript
复制
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);
        }
    }

我已经抓了一整天的头了,好像我错过了什么,任何帮助都会很感激。

更新

EN

回答 1

Stack Overflow用户

发布于 2015-04-17 10:21:46

您似乎还没有将网格配置为允许编辑。虽然添加了“创建”按钮,但需要更改可编辑字段以允许编辑,如下所示:

代码语言:javascript
复制
editable: {
            mode: "inline",
            create: true,
            update: true,
            destroy: true
        }

希望这有帮助..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29635052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档