1

全部,

尝试使用 Extjs 的 JSONStore 通过 POST 创建新记录时出现强制转换异常。当将空字符串传递给服务器并且服务器尝试将其转换为整数时,会发生异常。组合框的 valueField 设置为 int 定义的字段。数据存储字段如下:

  fields: [
            { name: 'id', type: 'int' },
            { name: 'displayOrder', mapping: 'displayOrder', type: 'int' },
            { name: 'displayName', mapping: 'displayName', type: 'string' },
            { name: 'enabled', mapping: 'enabled', type: 'boolean' },
            { name: 'letterCode', mapping: 'letterCode', type: 'string' }
        ],

组合框定义是:

 {
                xtype: 'combo',
                id:"secondaryIncidentCombo",
                hiddenName: 'secondaryIncidentTypeId',
                forceSelection: true,
                width:"200",
                selectOnFocus: true,
                emptyText: 'Secondary Incident',
                editable: false,
                mode: 'local',
                displayField:   'displayName',
                valueField:     'id',
                store: this.secondaryIncidentTypeArrayStore,
                triggerAction: 'all'
            },

奇怪的是,用于发送 POST 的 JSONStore 将组合框的值作为空字符串发送,即使我已将 JSONWriter 配置为不发送未更改的字段:

 writer: new Ext.data.JsonWriter({
            encode: false,   
            writeAllFields:false
        }),

发送到服务器的 POST 值: ....,"secondaryIncidentTypeId":"",...<-- 注意冒号后面的空字符串。

这是secondaryIncidentTypeArrayStore:

    secondaryIncidentTypeArrayStore: new Ext.data.ArrayStore({
    idProperty: 'id',
    fields: [
        { name: 'id', mapping: 'id', type: 'int' },
        { name: 'displayOrder', mapping: 'displayOrder', type: 'int' },
        { name: 'displayName', mapping: 'displayName', type: 'string' },
        { name: 'enabled', mapping: 'enabled', type: 'boolean' },
        { name: 'letterCode', mapping: 'letterCode', type: 'string' }
    ],
    data: []
})

我即将为空字符串编写手动检查,如果字符串为空,则将其设置为 null。这看起来很笨拙。在表单提交时向服务器发送任何内容或空值的正确方法是什么?

谢谢!

4

1 回答 1

1

发生的事情是因为您的id字段类型是integer将其强制转换为整数。因此,一个虚假值将是0

如果您删除类型,它将不会进行任何转换,并且在未定义时不会添加 id。

例如

new Ext.data.ArrayStore({
  idProperty: 'id',
  fields: [
    { name: 'id', mapping: 'id' },
    { name: 'displayOrder', mapping: 'displayOrder', type: 'int' },
    { name: 'displayName', mapping: 'displayName', type: 'string' },
    { name: 'enabled', mapping: 'enabled', type: 'boolean' },
    { name: 'letterCode', mapping: 'letterCode', type: 'string' }
  ],
  data: []
})

如果您必须发送id=null以使后端行为处理它的一种方法是添加一个自定义Ext.data.Type.

如果您不喜欢这些方法,也可以使用其他方法来解决它。

编辑:啊哈!试试allowNull物业。

于 2010-11-30T02:24:25.547 回答