Setting default values for a Model in MVC3

你。 提交于 2019-12-07 16:08:45

问题


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.ValidationMessageFor(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.

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


回答1:


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; }


来源:https://stackoverflow.com/questions/7141240/setting-default-values-for-a-model-in-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!