1

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

{"rowguid":"7e6dfa67-47a2-40fb-b3fb-9d21e9cba1c3","ProductId":100,"Name":"Car","Color":"#aaaaa","size":4,"Cost": 5.26,"Category":{"CatId":1,"Id":"f27b7d43-9b57-470e-97a3-023323a5a7ac","Name":"Vehicle"},"CategoryName":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,"名称":"飞机","颜色":"#aaaac","尺寸":6,"成本":7.26,"类别":{"CatId":1,"Id":"f5bd0e35-9ff4 -4a09-8b2b-a1f9c28fb60e","名称":"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 添加新记录。我认为如果我能够在添加新行过程中将模板与网格行附加或关联,那将是可能的。

让我知道是否可能。

4

1 回答 1

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())
      })
  ))
于 2013-06-26T20:12:49.527 回答