我的模型类中有以下数据注释:
[Required(ErrorMessage = "Required")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime DateOfBirth { get; set; }
当我在我看来使用以下内容时,我没有收到验证错误:
<tr>
<td>@Html.LabelFor(x => x.DateOfBirth, new { @class = "lbl" }, "Date Of Birth") </td>
<td>@Html.TextBoxFor(x => x.DateOfBirth, new { @class = "w100 _dob" })
<br>@Html.ValidationMessageFor(x => x.DateOfBirth)
</td>
</tr>
您能提出解决方案吗?
The DisplayFormat
属性与验证无关。它仅用于格式化显示在视图上的值。如果您想验证用户在相应输入字段中输入的值,您将必须编写自定义模型绑定程序。
顺便说一下, DisplayFormat 属性与 Html.EditorFor 帮助器结合使用,它对您正在使用的 Html.TextBoxFor 帮助器没有严格的影响:
<tr>
<td>
@Html.LabelFor(x => x.DateOfBirth, "Date Of Birth")
</td>
<td>
@Html.EditorFor(x => x.DateOfBirth)
<br/>
@Html.ValidationMessageFor(x => x.DateOfBirth)
</td>
</tr>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)