m-y*_*m-y 5 c# collections validation .net-4.5
基本上,如果我有一个对象集合,我如何将验证属性应用于集合中的每个项目(例如MaxLengthAttribute
)?
public class Foo
{
public ICollection<string> Bars { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
例如,如何确保Bars包含验证最大长度为256的字符串?
更新:
我理解如何在单个属性上应用验证属性,但问题是询问如何将它应用于集合中的对象.
public class Foo
{
[StringLength(256)] // This is obvious
public string Bar { get; set; }
// How do you apply the necessary attribute to each object in the collection!
public ICollection<string> Bars { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
NYC*_*Net -1
看一下数据注释功能:
这对你有用吗?
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public class Foo
{
[StringLength(256)]
public ICollection<string> Bars { get; set; }
}
Run Code Online (Sandbox Code Playgroud)