ASP.NET MVC 数据注释 - NullTextFormat
ASP.NET MVC DataAnnotation - NullTextFormat
我想创建一个表单,其中一个字段具有默认值,如下所示:
我创建了我的视图模型class:
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>
但结果此处没有默认文本:
为什么会这样,我做错了什么?
NullDisplayText
仅显示在 Html.DisplayFor
上,不显示在 Html.EditorFor
上。
您可以使用 HTML 元素的 placeholder
属性,并将其添加到 EditorFor
的 htmlAttributes
对象中:
@Html.EditorFor(model => model.CDLExpDate, new {
htmlAttributes = new {
@class = "form-control",
@placeholder = "MM/DD/YYYY"
}
})
我想创建一个表单,其中一个字段具有默认值,如下所示:
我创建了我的视图模型class:
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>
但结果此处没有默认文本:
为什么会这样,我做错了什么?
NullDisplayText
仅显示在 Html.DisplayFor
上,不显示在 Html.EditorFor
上。
您可以使用 HTML 元素的 placeholder
属性,并将其添加到 EditorFor
的 htmlAttributes
对象中:
@Html.EditorFor(model => model.CDLExpDate, new {
htmlAttributes = new {
@class = "form-control",
@placeholder = "MM/DD/YYYY"
}
})