DataAnnotation 和默认值
DataAnnotation and value by default
我有一个 属性 视图模型:
[Display(Name = "Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime DateTime { get; set; }
并查看:
<div class="form-group">
@Html.LabelFor(model => model.DateTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateTime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateTime, "", new { @class = "text-danger" })
</div>
</div>
并且页面在表单上显示默认值为 01/01/0001:
我想要没有任何默认值(用户应该指定正确的值,没有它就不能发送表单)。如何删除默认值?
您必须使 public System.DateTime DateTime { get; set; }
成为 nullable DateTime
.
我有一个 属性 视图模型:
[Display(Name = "Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public System.DateTime DateTime { get; set; }
并查看:
<div class="form-group">
@Html.LabelFor(model => model.DateTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DateTime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DateTime, "", new { @class = "text-danger" })
</div>
</div>
并且页面在表单上显示默认值为 01/01/0001:
我想要没有任何默认值(用户应该指定正确的值,没有它就不能发送表单)。如何删除默认值?
您必须使 public System.DateTime DateTime { get; set; }
成为 nullable DateTime
.