【问题标题】:Adding new row with Child properties in kendo grid在剑道网格中添加具有子属性的新行
【发布时间】:2013-06-05 22:39:12
【问题描述】:

我正在使用剑道网格 [web],并且我有以下 json 数据来呈现网格

{"rowguid":"7e6dfa67-47a2-40fb-b3fb-9d21e9cba1c3","ProductId":100,"Name":"Car","Color":"#aaaaa","size":4,"成本":5.26,"类别":{"CatId":1,"Id":"f27b7d43-9b57-470e-97a3-023323a5a7ac","名称":"车辆"},"类别名称":null},{" rowguid":"9cddd550-479c-4df8-883b-50f39e2b4249","ProductId":101,"Name":"bike","Color":"#aaaab","size":5,"Cost":6.26, "Category":{"CatId":1,"Id":"82842c66-223d-4774-a4b4-b748429f5984","Name":"2Wheeler"},"CategoryName":null},{"rowguid":"1a47a67f -ddff-401b-adee-b3bb27660e44","ProductId":102,"Name":"plane","Color":"#aaaac","size":6,"Cost":7.26,"Category":{ "CatId":1,"Id":"f5bd0e35-9ff4-4a09-8b2b-a1f9c28fb60e","Name":"AirCraft"},"CategoryName":null}]

从这个网格中可以看出,这个数据源是一个产品模型,它有一个称为 category 的内部属性,它又是另一个具有要使用的 name 属性的对象。

我使用了以下架构

  schema: {
            model: {
                id: "rowguid",
                fields: {
                    rowguid: { editable: false, nullable: true },
                    ProductId: { editable: true, nullable: true },
                    Name: { validation: { required: true} },
                    Color: { type: "string", validation: { required: true, min: 1} },
                    size: { type: "number", validation: { min: 1, required: true} },
                    Cost: { type: "number", validation: { min: 1, required: true }, format: "{0:n2}" }
                    ,CategoryName: {field:"Category.Name", type: "string", validation: { required: true }}
                }
            }
        }

以及以下列声明

  columns: [
            {field:"rowguid",tite:"Unique Id"},
            {field:"ProductId",tite:"Id"},
            {field:"Name",tite:"Name"},
            {field:"Color",tite:"Color", template:colorColumnTemplate},
            {field:"size",tite:"size",type:"number", format: "{0:n2}"},
            {field:"Cost",tite:"Cost",type:"number", format: "{0:n2}"},
            //{field:"CategoryName",title:"Category Name",editor:categoryEditor,template: "#=CategoryName#" },
            {field:"Category.Name",title:"Category Name",editor:categoryEditor},
            { command: { text: "Actions" }, title: "Action", width: "140px" },
            {command: ["edit"], title: "Actions", width: "172px"}
            ],            

在这里,网格显示正常,编辑、更新一切正常。

问题是当我点击“添加新记录”按钮时,出现以下错误

Uncaught TypeError: Cannot read property 'Name' of undefined

这是因为在形成剑道模型时,它会尝试将值读取为

javascript 对象属性events[idx].call(that, e); 是我在剑道网格中添加新行时触发的触发事件中的方法。

有没有办法让我使用 Category.Name 添加新记录。我认为如果我能够在添加新行过程中将模板与网格行附加或关联起来,那将是可能的。

如果可以,请告诉我。

【问题讨论】:

    标签: kendo-ui kendo-grid


    【解决方案1】:

    您需要为复杂字段添加一个 defaultValue,以声明复杂对象的空版本。 kendo 正在尝试设置 Category 的 Name 属性,但没有默认 Category 对象

    fields: [{ 
      field: Category
      defaultValue: { Name: ""}
    }]
    

    或在 MVC 帮助器中

      .Name("grid")
      .DataSource(dataSource => dataSource
          .Ajax().Model( model => {
            model.Field( m => m.MyField).DefaultValue(new MyObject())
          })
      ))
    

    【讨论】:

    • 使用 MVC 4 Razor 时的语法是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多