2013-11-25 25 views
0

我使用MVC 4,這是我的模型:日期驗證只是工程上的文字不是數字

[DisplayFormat(DataFormatString = "{0,d}")] 
[Required(ErrorMessage="The start date is required")] 
[Display(Name="Start Date")] 
public DateTime startDate { get; set; } 

當我在start date進入asdf,我得到了錯誤消息this is not a date但是當我進入02,我沒有收到錯誤消息。相反,當我使用modelstate

我在服務器中出現了state錯誤我正在使用{0,d}來驗證日期。

+1

您是否嘗試過加入'[數據類型(DataType.Date)]'。同時將你的'DisplayFormat'註釋更新爲'[DisplayFormat(ApplyFormatInEditMode = true,DataFormatString =「{0,d}」)]''看看是否有幫助。 –

+0

@StinkyTowel它仍然接受'02' –

+0

@ user1721236是的,我試過了,我重新回到你的答案 –

回答

0

我認爲你需要改變

[DisplayFormat(DataFormatString = "{0,d}")] 

[DisplayFormat(DataFormatString = "{0:d}")] 
+0

它仍然接受只是'02',我做了你的編輯,但它不適用於我 –

相關問題