多选择字符串列表保存时丢失值问题(版本21.214)
解决PXStringListAttribute多选模式下保存值清空问题
问题根源在于直接修改基类私有字段_AllowedValues和_AllowedLabels,绕过了PXStringListAttribute多选模式的核心初始化逻辑。以下是修正后的完整实现:
修正后的自定义属性代码
public class MyCustomAttribute : PXStringListAttribute { // 可选:显式指定多选分隔符,默认用逗号即可 private const string MultiSelectSeparator = ","; public MyCustomAttribute() : base(MultiSelectSeparator) { // 构造函数内默认启用多选(也可在DAC字段上单独设置) this.MultiSelect = true; } public override void CacheAttached(PXCache sender) { base.CacheAttached(sender); List<string> values = new List<string>(); List<string> labels = new List<string>(); PXResultset<MyCustomTable> results = PXSelect<MyCustomTable, Where<MyCustomTable.active, Equal<True>>> .Select(sender.Graph); foreach (MyCustomTable result in results) { values.Add(result.ValueID); labels.Add(result.Description); } // 用基类SetList方法设置选项,替换直接赋值私有字段的操作 this.SetList(values.ToArray(), labels.ToArray()); } }
关键修正说明
- 改用SetList方法:该方法会触发基类内部的多选逻辑初始化,确保选中的多个值能被正确拼接成字符串存入数据库,避免保存时的值丢失。
- 明确分隔符:构造函数传入分隔符(默认逗号),统一多选值的拼接规则,避免序列化/反序列化时出现异常。
- 字段长度校验:确认
nvarchar(255)的长度足够容纳拼接后的所有选中值(选项值+分隔符的总长度),若长度不足需调整字段类型的长度限制。
DAC字段配置示例
在数据访问类(DAC)的字段上应用属性时,确保开启多选配置:
[MyCustomAttribute(MultiSelect = true)] [PXDBString(255)] public virtual string MyCustomField { get; set; }
内容的提问来源于stack exchange,提问作者MikeNIke
相关产品推荐
相关产品推荐

