[英]required nullable DateTime
I recently started on a new project and came across the following.我最近开始了一个新项目,并遇到了以下问题。
I'm looking at a class Widget
which has a property:我正在查看具有以下属性的 class
Widget
:
[Required]
public int? WidgetTypeId { get; set; }
I'm struggling to see how this makes sense;我很难理解这有什么意义; if the field is
Required
why would it be nullable?如果该字段是
Required
的,为什么它可以为空? (I understand the answer may be "human error". I'm wondering if there is a legitimate reason for doing this.) (我知道答案可能是“人为错误”。我想知道这样做是否有正当理由。)
Data points:数据点:
The main reason is prevent under posting hacks.主要原因是防止发布黑客。 Also it displays an empty value in the view instead of the default value.
它还在视图中显示一个空值而不是默认值。
If it is non nullable DefaultModelBinder initializes with default value 01/01/0001 thus no ModelState error is generated.如果它不可为空,则 DefaultModelBinder 使用默认值 01/01/0001 进行初始化,因此不会生成 ModelState 错误。
In your case if an attack omits the property on a request a ModelState error is generated.在您的情况下,如果攻击省略了请求中的属性,则会生成 ModelState 错误。
Model Validation: Model Validation Model 验证: Model 验证
Under Posting Problem: Under and over posting problems过帐问题: 过帐和过帐问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.