MVC 集合的数据注释验证规则?
基于集合的属性是否有数据注释验证规则?
我有以下内容,
<DisplayName("Category")>
<Range(1, Integer.MaxValue, ErrorMessage:="Please select a category")>
Property CategoryId As Integer
<DisplayName("Technical Services")>
Property TechnicalServices As List(Of Integer)
我正在寻找一个验证器,我可以将其添加到 TechnicalServices 属性中以设置集合大小的最小值。
Is there a dataannotation validate rule for a collection based property?
I have the following
<DisplayName("Category")>
<Range(1, Integer.MaxValue, ErrorMessage:="Please select a category")>
Property CategoryId As Integer
<DisplayName("Technical Services")>
Property TechnicalServices As List(Of Integer)
I'm looking for a validator that i can add to the TechnicalServices property to set a minimum for the collection size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这样的事情可能会有所帮助:
还有改进的空间,但这是一个工作的开始。
I think something like this might help:
There's room for improvement, but that's a working start.
从 .NET 4 开始,另一个选择是让类本身(包含有问题的集合属性)实现 IValidatableObject,例如:
Validator 会自动为任何 IValidatableObjects 调用此 Validate 方法。
Another option from .NET 4 onward would be to make the class itself (which contains the collection property in question) implement IValidatableObject, such as:
The Validator in DataAnnotations will automatically call this Validate method for any IValidatableObjects.