简体   繁体   English

为什么不能禁用datetime必需属性的MVC模型?

[英]why can not disable datetime required attributes MVC model?

Every thing is ok This model: 一切正常,此模型:

[Range(1, 24, ErrorMessage = "Invalid Hour")]
public int? val1{ get; set; }

on the other hand; 另一方面; this returns invalid error. 这将返回无效错误。 if i dont write any value in EndDateTime: 如果我没有在EndDateTime中写入任何值:

public DateTime? EndDateTime { get; set; }

Controller: 控制器:

if (!ModelState.IsValid)
    return View(myinstance);

return invalid enddatetime But i give nullable value property. 返回无效的enddatetime但我给出了可为空的value属性。 i gave empty value on enddatetime, but return to me invalid enddatetime error.How to solve it? 我在enddatetime上给出了空值,但返回给我无效的enddatetime错误。如何解决?

It doesn't matter if the database accepts nulls. 数据库是否接受空无所谓。 The ModelState will not return valid if the value does not pass the validation. 如果值未通过验证,则ModelState将不会返回有效值。 Add a default value that will pass the validation. 添加将通过验证的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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