2011-08-21 95 views
3

這裏是我的模型的屬性:在MVC3模型設置的默認值

[Required(ErrorMessage = "Debe escribir su fecha de nacimiento")] 
[Display(Name = "Fecha de Nacimiento")] 
[DataType(DataType.Date)] 
public DateTime DateOfBirth { get; set; } 

在我的AccountController,我創建這個模型的一個新對象,並將其傳遞給視圖:

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

記住,此時這是一個Create窗體。該模型的屬性沒有值。

但是,這是呈現在我的HTML。

enter image description here

是否有某種方式來告訴視圖引擎不會使這一領域的東西嗎?

回答

4

如果您將您的定義更改爲Nullable,那麼它將從null開始,如果沒有設置值(默認情況下不是),則不會顯示任何值。

public DateTime? DateOfBirth { get; set; }