【问题标题】:ComboBox: Empty string sent on POST when it's valueField is numeric. How can I pass null to Server?ComboBox:当它的 valueField 是数字时,在 POST 上发送的空字符串。如何将 null 传递给服务器?
【发布时间】:2011-05-17 11:00:47
【问题描述】:

全部,

我在尝试使用 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。这看起来很笨拙。在表单提交时向服务器发送任何内容或空值的正确方法是什么?

谢谢!

【问题讨论】:

    标签: combobox extjs jsonstore


    【解决方案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 属性。

    【讨论】:

    • 我尝试删除组合中使用的值字段的类型声明,但没有效果。创建自定义类型是可能的,我很惊讶框架还没有处理这个问题。感谢您的输入!
    • 已编辑以显示我的意思 - 你这样做了,它仍然将 null 转换为 0
    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 2016-02-10
    相关资源
    最近更新 更多