【发布时间】:2023-03-09 15:33:01
【问题描述】:
我想创建一个表单,其中一个字段具有默认值,如下所示:
我创建了我的视图模型类:
public class ApplicationDriverLicenseVM
{
[Required]
[Display(Name = "CDL Expiration Date")]
[DisplayFormat(ConvertEmptyStringToNull = true, NullDisplayText = "MM/DD/YYYY")]
public DateTime? CDLExpDate { get; set; }
和视图(通过脚手架):
<div class="form-group">
@Html.LabelFor(model => model.CDLExpDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CDLExpDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CDLExpDate, "", new { @class = "text-danger" })
</div>
</div>
但结果这里没有默认文本:
为什么会这样,我做错了什么?
【问题讨论】:
-
我相信您可能正在寻找占位符。
new { htmlAttributes = new { @class = "form-control", placeholder = "MM/DD/YYYY" } -
NullDisplayText仅在您使用@Html.DisplayFor()时才会受到尊重
标签: asp.net-mvc data-annotations