通用列表属性的必需属性
是否可以将 [Required] 属性放入 List<> 上?财产?
我绑定到 POST 上的通用列表,并且想知道如果属性中有 0 个项目,是否可以使 ModelState.IsValid() 失败?
Is it possible to put a [Required] attribute onto a List<> property?
I bind to a generic list on POST and was wondering if I could make ModelState.IsValid() fail if the property has 0 items in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将
Required
属性添加到列表样式属性并不能真正实现您想要的效果。如果未创建列表,则会抱怨,但如果列表中存在 0 个项目,则不会抱怨。但是,派生您自己的数据注释属性并使其检查列表中的
Count
> 应该很容易。 0.类似这样的东西(尚未测试):编辑:
您还必须小心如何在视图中绑定列表。例如,如果将
List
绑定到如下所示的视图:MVC 模型绑定器将始终在列表中放入 5 个元素,全部为
String。空
。如果这就是您的视图的工作方式,您的属性将需要变得更复杂一些,例如使用反射来提取通用类型参数并将每个列表元素与default(T)
或其他内容进行比较。更好的替代方法是使用 jQuery 动态创建输入元素。
Adding the
Required
attribute to a list-style property doesn't really do what you want. The will complain if the list isn't created, but won't complain if the list exists with 0 item in it.However, it should be easy enough to derive your own data annotations attribute and make it check the list for
Count
> 0. Something like this (not tested yet):EDIT:
You'll also have to be careful how you bind your list in your view. For example, if you bind a
List<String>
to a view like this:The MVC model binder will always put 5 elements in your list, all
String.Empty
. If this is how your View works, your attribute would need to get a bit more complex, such as using Reflection to pull the generic type parameter and comparing each list element withdefault(T)
or something.A better alternative is to use jQuery to create the input elements dynamically.
对于那些正在寻找简约示例的人:
这是对已接受答案的修改后的代码。它适用于问题中的情况,甚至更多情况下,因为 IEnumerable 在 System.Collections 层次结构中较高。此外,它继承了RequiredAttribute 的行为,因此无需显式编码。
For those who're looking for minimalist examples:
This is modified code from the accepted answer. It is suitable in the case from the question, and in even more cases, since IEnumerable is higher in System.Collections hierarchy. Additionally, it inherits behavior from RequiredAttribute, so no need in coding it explicitly.
对于那些使用 C# 6.0(及更高版本)并且正在寻找俏皮话的人:
For those that use C# 6.0 (and above) and who are looking for one-liners:
根据我的要求修改了 @moudrick 实现
列表和复选框列表所需的验证属性
如果您有复选框列表
这是我的视图模型
用法
Modified @moudrick implementation for my requirement
Required Validation Attribute for List and checkbox List
If you have checkbox list
Here is my View Model
Usage