You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor绑定EntityItemValue的object属性如何基于TypeConverter解决转换报错

方案1:手动实现双向绑定(最易落地)

放弃Blazor的@bind语法糖,拆分绑定值和变更事件,手动根据EntityItem.Type做类型转换,代码实现如下:

<div class="flex-container">
    <div class="input-style">
        <input type="text" 
               value="@Param.Value?.ToString()" 
               @onchange="HandleValueChange" />
    </div>
</div>

@code {
    [Parameter]
    public EntityItemValue Param { get; set; }

    private void HandleValueChange(ChangeEventArgs e)
    {
        var inputStr = e.Value?.ToString();
        // 空输入直接赋值为null
        if (string.IsNullOrWhiteSpace(inputStr))
        {
            Param.Value = null;
            return;
        }
        // 获取目标类型对应的转换器
        var typeConverter = TypeDescriptor.GetConverter(Param.Item.Type);
        if (typeConverter.CanConvertFrom(typeof(string)))
        {
            try
            {
                // 转换成功后赋值给Value
                Param.Value = typeConverter.ConvertFromString(inputStr);
            }
            catch
            {
                // 转换失败可自定义逻辑,比如保留原值/提示输入错误
                Param.Value = null;
            }
        }
        else
        {
            // 不支持字符串转换的类型默认赋值为原始输入字符串
            Param.Value = inputStr;
        }
    }
}

方案2:按类型动态渲染输入组件(体验更好)

可以根据EntityItem.Type渲染对应类型的原生输入控件,减少格式转换错误:

<div class="flex-container">
    <div class="input-style">
        @switch (Param.Item.Type)
        {
            case Type t when t == typeof(int) || t == typeof(decimal) || t == typeof(double):
                <input type="number" @bind="NumericValue" />
                break;
            case Type t when t == typeof(bool):
                <input type="checkbox" @bind="BoolValue" />
                break;
            case Type t when t == typeof(DateTime):
                <input type="date" @bind="DateValue" />
                break;
            default:
                <input type="text" @bind="StringValue" />
                break;
        }
    </div>
</div>

@code {
    [Parameter]
    public EntityItemValue Param { get; set; }

    // 中间属性做类型适配
    private object NumericValue
    {
        get => Param.Value ?? 0;
        set => Param.Value = Convert.ChangeType(value, Param.Item.Type);
    }

    private bool BoolValue
    {
        get => Param.Value is bool b && b;
        set => Param.Value = value;
    }

    private DateTime DateValue
    {
        get => Param.Value is DateTime dt ? dt : DateTime.Now;
        set => Param.Value = value;
    }

    private string StringValue
    {
        get => Param.Value?.ToString() ?? string.Empty;
        set => Param.Value = value;
    }
}

自定义类型处理

如果EntityItem.Type是你自己实现的自定义类型,直接给该类型加TypeConverterAttribute注册对应的转换器即可,无需修改全局配置:

[TypeConverter(typeof(MyCustomTypeConverter))]
public class MyCustomType
{
    // 类定义
}

public class MyCustomTypeConverter : TypeConverter
{
    // 实现从字符串转换的逻辑
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        if (value is string str)
        {
            // 自定义转换逻辑
            return new MyCustomType(str);
        }
        return base.ConvertFrom(context, culture, value);
    }
}

内容的提问来源于stack exchange,提问作者Furqan Safdar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 23:15:03