我已經在我的模型類以下dataannotation:DisplayFormat Dataannotation不工作
[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>
能否請你建議方案?
我注意到'Html.TextBoxFor'爲我正在處理的項目中的一個EF POCO,'DisplayFormat'正常工作。但是,如果我嘗試使用此字段的編輯視圖模型我正在使用相同的副本完全一樣'DisplayFormat',現場使用默認的顯示格式。然而,獨特的'DisplayName'呢,這也是我怎麼知道它的使用這套DataAnnotations'的'(即我追加「VMTest」到'DisplayName')。這可能是我項目中某些具體事情的後果,但這是一個有趣的怪癖,這使我感到困惑。 –