设置默认值在MVC3模型 [英] Setting default values for a Model in MVC3

查看:370
本文介绍了设置默认值在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; }

在我的AccountController,我创建了这个模型的一个新对象,并将其传递给视图:

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.

然而,这是呈现在我的HTML

However, this is rendered in my HTML.

有一些方法来告诉视图引擎不会使这一领域的东西吗?

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

这篇关于设置默认值在MVC3模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆