diff --git a/Admin.NET/Admin.NET.Core/Attribute/DictAttribute.cs b/Admin.NET/Admin.NET.Core/Attribute/DictAttribute.cs
index 7de910d4..04d98be2 100644
--- a/Admin.NET/Admin.NET.Core/Attribute/DictAttribute.cs
+++ b/Admin.NET/Admin.NET.Core/Attribute/DictAttribute.cs
@@ -13,6 +13,21 @@ namespace Admin.NET.Core;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
public class DictAttribute : ValidationAttribute, ITransient
{
+ ///
+ /// 字典编码
+ ///
+ public string DictTypeCode { get; }
+
+ ///
+ /// 是否允许空字符串
+ ///
+ public bool AllowEmptyStrings { get; set; } = false;
+
+ ///
+ /// 允许空值,有值才验证,默认 false
+ ///
+ public bool AllowNullValue { get; set; } = false;
+
///
/// 字典值合规性校验特性
///
@@ -43,27 +58,16 @@ public class DictAttribute : ValidationAttribute, ITransient
var sysDictDataServiceProvider = App.GetRequiredService();
var dictDataList = sysDictDataServiceProvider.GetDataList(DictTypeCode).Result;
+ // 获取枚举类型,可能存在Nullable类型,所以需要尝试获取最终类型
+ var type = value?.GetType();
+ type = type != null ? Nullable.GetUnderlyingType(type) ?? type : null;
+
// 使用HashSet来提高查找效率
- var valueList = (value?.GetType().IsEnum ?? DictTypeCode.EndsWith("Enum")) ? dictDataList.Select(u => u.Name) : dictDataList.Select(u => u.Code);
+ var valueList = (type?.IsEnum ?? DictTypeCode.EndsWith("Enum")) ? dictDataList.Select(u => u.Name) : dictDataList.Select(u => u.Value);
var dictHash = new HashSet(valueList);
if (!dictHash.Contains(valueAsString))
return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!");
return ValidationResult.Success;
}
-
- ///
- /// 字典编码
- ///
- public string DictTypeCode { get; set; }
-
- ///
- /// 是否允许空字符串
- ///
- public bool AllowEmptyStrings { get; set; } = false;
-
- ///
- /// 允许空值,有值才验证,默认 false
- ///
- public bool AllowNullValue { get; set; } = false;
}
\ No newline at end of file
diff --git a/Admin.NET/Admin.NET.Core/Entity/SysDictData.cs b/Admin.NET/Admin.NET.Core/Entity/SysDictData.cs
index 4c597937..4d8fe9eb 100644
--- a/Admin.NET/Admin.NET.Core/Entity/SysDictData.cs
+++ b/Admin.NET/Admin.NET.Core/Entity/SysDictData.cs
@@ -31,8 +31,8 @@ public partial class SysDictData : EntityBase
///
/// 值
///
- [SugarColumn(ColumnDescription = "值", Length = 128)]
- [Required, MaxLength(128)]
+ [SugarColumn(ColumnDescription = "值", Length = 256)]
+ [Required, MaxLength(256)]
public virtual string Value { get; set; }
///
diff --git a/Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs b/Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs
index 87235584..6c1e6803 100644
--- a/Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs
+++ b/Admin.NET/Admin.NET.Core/Enum/ErrorCodeEnum.cs
@@ -310,7 +310,7 @@ public enum ErrorCodeEnum
///
/// 字典值已存在
///
- [ErrorCodeItemMetadata("字典值已存在,名称或编码重复")]
+ [ErrorCodeItemMetadata("字典值已存在")]
D3003,
///
diff --git a/Admin.NET/Admin.NET.Core/Utils/ExcelHelper.cs b/Admin.NET/Admin.NET.Core/Utils/ExcelHelper.cs
index 28562931..6d93ee09 100644
--- a/Admin.NET/Admin.NET.Core/Utils/ExcelHelper.cs
+++ b/Admin.NET/Admin.NET.Core/Utils/ExcelHelper.cs
@@ -33,7 +33,7 @@ public class ExcelHelper
for (int i = 0; i < rows.Count; i++) pageItems[i].Id = rows[i].Id;
for (int i = 0; i < storageable.TotalList.Count; i++)
- pageItems[i].Error = storageable.TotalList[i].StorageMessage;
+ pageItems[i].Error ??= storageable.TotalList[i].StorageMessage;
}
}));
});
@@ -59,7 +59,7 @@ public class ExcelHelper
}
///
- /// 导出Xlsx数据
+ /// 导出xlsx数据
///
///
///
@@ -72,7 +72,7 @@ public class ExcelHelper
}
///
- /// 根据类型导出Xlsx模板
+ /// 根据类型导出xlsx模板
///
///
///
@@ -98,9 +98,27 @@ public class ExcelHelper
if (++columnIndex > 0 && item.Text.Equals(headerAttr.DisplayName)) break;
if (columnIndex <= 0) continue;
- // 优先从代理函数中获取下列列表,若为空且字段为枚举型,则填充枚举项为下列列表,否则不设置下列列表
+ // 优先从代理函数中获取下列列表,若为空且字段为枚举型,则填充枚举项为下列列表,若为字典字段,则填充字典值value列表为下列列表
var dataList = addListValidationFun?.Invoke(worksheet, prop)?.ToList();
- if (dataList == null && propType.IsEnum()) dataList = propType.EnumToList()?.Select(it => it.Describe).ToList();
+ if (dataList == null)
+ {
+ if (propType.IsEnum())
+ {
+ // 填充枚举项为下列列表
+ dataList = propType.EnumToList()?.Select(u => u.Describe).ToList();
+ }
+ else
+ {
+ // 获取字段上的字典特性
+ var dict = prop.GetCustomAttribute();
+ if (dict != null)
+ {
+ // 填充字典值value为下列列表
+ dataList = App.GetService().GetDataList(new GetDataDictTypeInput
+ { Code = dict.DictTypeCode }).Result?.Select(u => u.Value).ToList();
+ }
+ }
+ }
if (dataList != null) AddListValidation(columnIndex, dataList);
}