开发者

Setting default values for a Model in MVC3

Here's my model's property:

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

In my AccountController, I create a new object of this model and pass it to the View:

<div class="editor-label">
    @Html.LabelFor(model => model.DateOfBirth)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.DateOfBirth)
    @Html.Vali开发者_如何转开发dationMessageFor(model => model.DateOfBirth)
</div>

Remember, at this point this is a Create form. The model's properties have no values.

However, this is rendered in my HTML.

Setting default values for a Model in MVC3

Is there some way to tell the view engine to not render anything in this field?


If you change your definition to a Nullable, then it'll start off as null and no value will be displayed if one isn't set (which by default it isn't).

public DateTime? DateOfBirth { get; set; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜