2011-08-21 129 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; }