【问题标题】:Entity Framework date only property format实体框架仅日期属性格式
【发布时间】:2021-11-11 07:30:39
【问题描述】:

我正在使用 Entity Framework Core 创建一个包含生日列的简单用户表。

    [Column(TypeName="Date")]
    [Display(Name = "Data de nascimento")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
    public DateTime BirthDate { get; set; }

然后它成功地在详细信息页面上只显示日期而不是时间,但是当我尝试编辑用户时,输入框要求我输入 DateTime 而不仅仅是日期。

【问题讨论】:

  • 你能分享一下输入和错误吗?

标签: c# asp.net-mvc entity-framework date


【解决方案1】:

听起来像是选择了错误的编辑器模板。尝试将DataType 属性添加到属性:

[Column(TypeName="Date")]
[Display(Name = "Data de nascimento")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
[DataType(DataType.Date)]
public DateTime BirthDate { get; set; }

DataTypeAttribute Class
DataType Enum

【讨论】:

  • 成功了。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
相关资源
最近更新 更多