2010-12-04 63 views
6

在我的視圖模型我有以下屬性:ASP.net MVC數據註釋的DateTime默認值

[Required] 
[DataType(DataType.Date, ErrorMessage="Please enter a valid date in the format dd/mm/yyyy")] 
[Display(Name = "Date of Birth")] 
public DateTime DOB { get; set; } 

在我看來,我有以下幾點:

<div class="editor-label"> 
    @Html.LabelFor(model => model.DOB) 
</div> 
<div class="editor-field"> 
    @Html.EditorFor(model => model.DOB) 
    @Html.ValidationMessageFor(model => model.DOB) 
</div> 

提交表單的默認前DOB的值是1/01/0001我如何阻止這個值被自動填充,當人們訪問這個表單時我只想要一個空的字段?

回答

7

我相信你將不得不使用可空的DateTime?類型。 DateTime不能爲null,因此它始終有一個值。

6

嘗試使DOB日期時間爲空的像@Mayo指出:

public DateTime? DOB { get; set; } 
1

日期時間型結構的。所以,默認情況下,DateTime不能爲空。它的默認值等於'01/01/0001'。 解決您的問題的方法是使用可空的DateTime?類型。 如果您希望它默認爲'01/01/2014'等值,那麼您可以指定如下值: DOB = new DateTime(2014,01,01);