gpt4 book ai didi

asp.net-mvc-3 - 在 MVC3 中为模型设置默认值

转载 作者:行者123 更新时间:2023-12-01 12:59:54 25 4
gpt4 key购买 nike

这是我模型的属性:

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

在我的 AccountController 中,我创建了这个模型的一个新对象并将其传递给 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>

请记住,此时这是一个创建表单。模型的属性没有值。

但是,这是在我的 HTML 中呈现的。

enter image description here

有什么方法可以告诉 View 引擎不要渲染该字段中的任何内容吗?

最佳答案

如果您将定义更改为 Nullable,那么它将以 null 开始,如果未设置(默认情况下未设置)则不会显示任何值。

public DateTime? DateOfBirth { get; set; }

关于asp.net-mvc-3 - 在 MVC3 中为模型设置默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7141240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com