我在这里看到了问题,但我无法解决问题。mvc 5验证过去的日期
我希望当用户在表格中插入日期时只有过去的日期才是相关的。
例如:
用户不能在02/03/2016后插入日期。
我的模型:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
[Display(Name = "Date")]
[Required(ErrorMessage = "Date is mandatory")]
public DateTime created_date { get; set; }
我的观点:
<div class="form-group" >
@Html.LabelFor(model => model.created_date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10" >
@Html.EditorFor(model => model.created_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.created_date, "", new { @class = "text-danger" })
</div>
</div>
感谢。
附注:既然你似乎想要HTML5的浏览器日期选择器(这仅适用于Chrome和边缘支持) ,格式必须是'DataFormat String =“{0:yyyy-MM-dd}”'为了正常工作。您应该考虑使用自定义验证属性来获取客户端和服务器端验证。 –