【问题标题】:Change DateTime error message更改日期时间错误消息
【发布时间】:2015-06-01 13:05:55
【问题描述】:

我使用 MVC 5,在我的模型中我有这样的 DateTime 属性:

[Column(TypeName = "Date")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }

当我输入无效日期时,ValidationSummary 中会显示以下消息:

StartDate 字段必须是日期

我需要在哪里更改此消息?

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    你需要添加ErrorMessage属性。试试这个

    [DataType(DataType.Date),ErrorMessage="Your message here"]
    

    【讨论】:

    • 看起来很明显,但这不起作用。 stackoverflow.com/questions/13916991/mvc-datatype-errormessage
    • @CodeCaster 是 ErrorMessage 不适用于我,但您发送的链接也没有帮助我,请我需要更多说明 DataType 中 ErrorMessage 的用途以及如何覆盖它默认消息
    • @AbrahamJosef 向下滚动一点到 CodeCaster 提供的链接中的下一个答案。它展示了如何使用ValidationMessageFor。嗯……
    【解决方案2】:

    对我有用的是将 data-val-date 属性添加到文本框以覆盖为其创建的属性(检查文本框,您会看到)。

    @Html.TextBoxFor(model => model.DateOfBirth, new { @class = "form-control", inputDate = "", data_val_date = "Please enter a valid date" })
    

    【讨论】:

      【解决方案3】:

      从剃须刀你可以这样做。

      @Html.ValidationMessageFor(model => model.StartDate, "Your custom message.", new { @class = "text-danger" })
      

      【讨论】:

      • 当我这样做时,无论数据是否有效,消息总是显示。
      猜你喜欢
      • 2015-02-26
      • 1970-01-01
      • 2021-12-14
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      • 1970-01-01
      相关资源
      最近更新 更多