【发布时间】:2016-06-15 21:48:00
【问题描述】:
我在这里看到了问题,但我无法解决问题。
我希望当用户在表单中插入日期时,只有过去的日期才是相关的。
例子:
用户不能在 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 和 Edge 中支持),格式必须为
DataFormatString = "{0:yyyy-MM-dd}"才能正常工作。您应该考虑使用自定义验证属性来获取客户端和服务器端验证。
标签: c# html asp.net asp.net-mvc asp.net-mvc-5