【无标题】ext.net core 为 Ext.TextField 的 change 事件添加一个 AJAX 请求,在用户更改客户名称时,实时向后端发送请求验证是否重复

<ext-textfield fieldLabel="客户名称" name="FullName" labelAlign="Right">
    <listeners>
        <change handler="
            var newValue = this.getValue();

            if (!newValue) {
                return; // 如果没有输入值,不发送请求
            }

            Ext.Ajax.request({
                url: '/CustomerManagement/CheckCustomerName',
                method: 'POST',
                params: { customerName: newValue },
                success: function (response) {
                    var result = Ext.decode(response.responseText);

                    if (!result.success) {
                        Ext.Msg.alert('重复提醒', result.message);
                    }
                },
                failure: function () {
                    Ext.Msg.alert('错误', '检查客户名称时出错,请稍后重试。');
                }
            });
        " />
    </listeners>
</ext-textfield>
[HttpPost]
public JsonResult CheckCustomerName(string customerName)
{
    // 查询数据库判断是否存在相同名称
    var isDuplicate = Gttc.CustomerOrgs.Any(x => x.FullName == customerName);

    if (isDuplicate)
    {
        return Json(new 
        { 
            success = false, 
            message = $"客户名称 '{customerName}' 已存在,请使用其他名称。" 
        });
    }

    return Json(new 
    { 
        success = true 
    });
}

前端监听 change 事件:

在用户更改输入内容时,实时触发验证逻辑。
使用 Ext.Ajax.request 异步调用后端。
后端检测逻辑:

检查数据库中是否已经存在相同的客户名称。
如果存在,返回 success: false 和错误提示消息。
如果不存在,返回 success: true。
错误处理:

如果请求失败(网络问题或后端错误),前端弹出统一的错误提示框。
防止无效请求:

在前端判断输入值是否为空,避免发送无意义的请求。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值