C# 对IEnumerable(字符串集合)的验证
这只能通过一个自定义验证器来完成,还是我遗漏了什么?只需简单检查C# 对IEnumerable(字符串集合)的验证,c#,asp.net-core,blazor,blazor-client-side,C#,Asp.net Core,Blazor,Blazor Client Side,这只能通过一个自定义验证器来完成,还是我遗漏了什么?只需简单检查ICollection属性是否至少有一项 尝试此操作时运气不佳: [必需] [MinLength(1,ErrorMessage=“至少需要一个东西”)] 公共ICollection something{get;set;} 谢谢 一个选项是向模型中添加一个额外的属性,用于计算集合的长度,然后根据该属性进行验证: public ICollection<string> Somethings { get; set; } [R
ICollection
属性是否至少有一项
尝试此操作时运气不佳:
[必需]
[MinLength(1,ErrorMessage=“至少需要一个东西”)]
公共ICollection something{get;set;}
谢谢 一个选项是向模型中添加一个额外的属性,用于计算集合的长度,然后根据该属性进行验证:
public ICollection<string> Somethings { get; set; }
[Range(1, 9999, ErrorMessage = "At least one Something is required")]
public int SomethingsCount => Somethings == null ? 0 : Somethings.Count;
像这样使用它
[RequiredCollection(ErrorMessage = "At least one Something is required")]
public ICollection<string> Somethings { get; set; }
[RequiredCollection(ErrorMessage=“至少需要一件东西”)]
公共ICollection something{get;set;}
这是一个实用的实施示例:
class Program
{
static void Main(string[] args)
{
var iceCream = new BuyIcecreamRequest { Tastes = new List<string>() { "Chocolate" } };
var results = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(iceCream, new ValidationContext(iceCream), results, true);
}
}
public class MinimumCollectionLength : ValidationAttribute
{
private readonly int _minimumCollectionLength;
public MinimumCollectionLength(int minimumCollectionLength)
{
_minimumCollectionLength = minimumCollectionLength;
}
public override bool IsValid(object value)
{
var collection = value as ICollection;
if (collection != null)
{
return collection.Count >= _minimumCollectionLength;
}
return false;
}
}
public class BuyIcecreamRequest
{
[Required]
[MinimumCollectionLength(1, ErrorMessage = "At least one Taste is required")]
public ICollection<string> Tastes { get; set; }
}
类程序
{
静态void Main(字符串[]参数)
{
var iceCream=new-buyeicecreamrequest{Tastes=new-List(){“Chocolate”};
var results=新列表();
bool isValid=Validator.TryValidateObject(冰激凌,新的ValidationContext(冰激凌),结果,true);
}
}
公共类最小集合长度:ValidationAttribute
{
私有只读int_minimumCollectionLength;
公共最小集合长度(int MinimumCollectionLength)
{
_minimumCollectionLength=minimumCollectionLength;
}
公共覆盖布尔值有效(对象值)
{
var collection=作为ICollection的值;
if(集合!=null)
{
返回集合。计数>=\u最小集合长度;
}
返回false;
}
}
公共类购买请求
{
[必需]
[MinimumCollectionLength(1,ErrorMessage=“至少需要一种口味”)]
公共ICollection{get;set;}
}
您可以使用此自定义验证器:我使用了此验证器。只是想确保没有任何内置的方法我错过了,但这是一个简短的,我现在需要的。谢谢巴西特汉克斯回答马可的问题。如果相关的话,我可能会在将来切换到类似这样的选项。
class Program
{
static void Main(string[] args)
{
var iceCream = new BuyIcecreamRequest { Tastes = new List<string>() { "Chocolate" } };
var results = new List<ValidationResult>();
bool isValid = Validator.TryValidateObject(iceCream, new ValidationContext(iceCream), results, true);
}
}
public class MinimumCollectionLength : ValidationAttribute
{
private readonly int _minimumCollectionLength;
public MinimumCollectionLength(int minimumCollectionLength)
{
_minimumCollectionLength = minimumCollectionLength;
}
public override bool IsValid(object value)
{
var collection = value as ICollection;
if (collection != null)
{
return collection.Count >= _minimumCollectionLength;
}
return false;
}
}
public class BuyIcecreamRequest
{
[Required]
[MinimumCollectionLength(1, ErrorMessage = "At least one Taste is required")]
public ICollection<string> Tastes { get; set; }
}