瀏覽代碼

在字典验证失败时,返回的ValidationResult 里传入字段名称(即中文名称), 解决当数据表中有字典类型的字段时,从excel导入报错的问题(One or more errors occurred. (导入异常:System.InvalidOperationException: Sequence contains no elements at System.Linq.ThrowHelper.ThrowNoElementsException() at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) at Magicodes.ExporterAndImporter.Excel.Utility.ImportHelper`1.Import(String filePath, Func`2 importResultCallback)))

hwjmyz 10 月之前
父節點
當前提交
ce6da25012
共有 1 個文件被更改,包括 18 次插入4 次删除
  1. 18 4
      Admin.NET/Admin.NET.Core/Attribute/DictAttribute.cs

+ 18 - 4
Admin.NET/Admin.NET.Core/Attribute/DictAttribute.cs

@@ -54,6 +54,8 @@ public class DictAttribute : ValidationAttribute, ITransient
         var property = validationContext.ObjectType.GetProperty(validationContext.MemberName!);
         if (property == null) return new ValidationResult($"未知属性: {validationContext.MemberName}");
 
+        string importHeaderName = GetImporterHeaderName(property, validationContext.MemberName);
+
         var propertyType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
 
         // 先尝试从 ValidationContext 的依赖注入容器中拿服务,拿不到或类型不匹配时,再从全局的 App 容器中获取
@@ -88,7 +90,7 @@ public class DictAttribute : ValidationAttribute, ITransient
                     if (item == null && AllowNullValue) continue;
 
                     if (!Enum.IsDefined(underlyingElementType, item!))
-                        return new ValidationResult($"提示:{ErrorMessage}|枚举值【{item}】不是有效的【{underlyingElementType.Name}】枚举类型值!");
+                        return new ValidationResult($"提示:{ErrorMessage}|枚举值【{item}】不是有效的【{underlyingElementType.Name}】枚举类型值!", [importHeaderName]);
                 }
                 return ValidationResult.Success;
             }
@@ -99,7 +101,7 @@ public class DictAttribute : ValidationAttribute, ITransient
 
                 var itemString = item?.ToString();
                 if (!dictHash.Contains(itemString))
-                    return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{itemString}】!");
+                    return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{itemString}】!", [importHeaderName]);
             }
 
             return ValidationResult.Success;
@@ -113,12 +115,24 @@ public class DictAttribute : ValidationAttribute, ITransient
         // 枚举类型验证
         if (propertyType.IsEnum)
         {
-            if (!Enum.IsDefined(propertyType, value!)) return new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!");
+            if (!Enum.IsDefined(propertyType, value!)) return new ValidationResult($"提示:{ErrorMessage}|枚举值【{value}】不是有效的【{propertyType.Name}】枚举类型值!", [importHeaderName]);
             return ValidationResult.Success;
         }
 
-        if (!dictHash.Contains(valueAsString)) return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!");
+        if (!dictHash.Contains(valueAsString))
+            return new ValidationResult($"提示:{ErrorMessage}|字典【{DictTypeCode}】不包含【{valueAsString}】!", [importHeaderName]);
 
         return ValidationResult.Success;
     }
+
+    /// <summary>
+    /// 获取本字段上 [ImporterHeader(Name = "xxx")] 里的Name,如果没有则使用defaultName.
+    /// 用于在从excel导入数据时,能让调用者知道是哪个字段验证失败,而不是抛异常
+    /// </summary>
+    private static string GetImporterHeaderName(PropertyInfo property, string defaultName)
+    {
+        var importerHeader = property.GetCustomAttribute<ImporterHeaderAttribute>();
+        string importerHeaderName = importerHeader?.Name ?? defaultName;
+        return importerHeaderName;
+    }
 }