简体   繁体   English

必需的可为空的日期时间

[英]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:数据点:

  • I haven't done an exhaustive analysis but the UI (the bits with which I am familiar at any rate) appears to require the field to be populated.我没有进行详尽的分析,但 UI(我无论如何都熟悉的位)似乎需要填充该字段。
  • There is a backing database table, and the database column in question is not nullable.有一个后备数据库表,并且有问题的数据库列不可为空。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM