在我的模型中,我的其中一个属性具有以下DataAnnotations

[Required(ErrorMessage = "*")]
[DisplayFormat(DataFormatString = "{0:d}")]
[DataType(DataType.Date)]
public DateTime Birthdate { get; set; }

所需的注释效果很好,我添加了其他两个以节省时间。它使用以下方式绑定(bind)到 View 中的输入
<%=Html.TextBoxFor(m => m.Birthdate, new { @class = "middle-input" })%>

但是,无论何时加载 View ,我仍然会在输入框中显示时间。无论如何,有使用DataAnnotations删除它吗?

最佳答案

[DisplayFormat]属性仅在EditorFor / DisplayFor中使用,而未由诸如TextBoxFor之类的原始HTML API使用。

关于asp.net-mvc - ASP.Net MVC DisplayFormat,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2001756/

10-11 05:14
查看更多