【问题标题】:Custom error message with DataAnnotationsExtensions带有 DataAnnotationsExtensions 的自定义错误消息
【发布时间】:2013-08-08 09:29:10
【问题描述】:

我正在尝试让 Scott Kirkland 的 DataAnnotationsExtensions 与我的 MVC4 项目一起使用。但是我在客户端验证电子邮件地址时遇到问题。我添加了带有错误消息的 EmailAddress 注释,但是当我输入无效的电子邮件地址时,我没有收到自定义错误消息,而是收到通用电子邮件错误消息“请输入有效的 RecipientEmail 地址。”。

我的班级是这样的:

public class NpRequest
{
    [DisplayName("Telefonnummer som skal overdrages")]
    [Required(ErrorMessage = "Angiv telefonnummeret som skal overdrages")]
    public string PhoneNumer { get; set; }

    [DisplayName("Recipient email address")]
    [EmailAddress(ErrorMessage = "This is my custom error message")]
    [Required(ErrorMessage = "The recipient email address is required")]
    public string RecipientEmail { get; set; }
    public RecipientTypeEnum RecipientType { get; set; }
}

我的看法:

---片段开始---

    <div class="editor-label">
        @Html.LabelFor(model => model.PhoneNumer)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.PhoneNumer)
        @Html.ValidationMessageFor(model => model.PhoneNumer)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.RecipientEmail)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.RecipientEmail)
        @Html.ValidationMessageFor(model => model.RecipientEmail)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>

---片段结束---

编辑: 当我检查 HTML 时,它看起来像这样:

<input class="text-box single-line input-validation-error" data-val="true" data-val-email="This is my custom error message" data-val-required="The recipient email address is required" id="RecipientEmail" name="RecipientEmail" type="email" value="">

似乎我的自定义错误消息已放入 data-val-email 属性中。我的印象是 DataAnnotationExtension 自动将我的自定义错误消息添加到 ModelState 中,从而也将其添加到 field-validation-error span 中,这显示了 MVC 验证错误。

这个假设是错误的吗?我是否应该编写自己的 javascript,提取自定义错误消息属性并将其注入到 field-validation-error span 中?

谁能看出我做错了什么?

【问题讨论】:

  • 您是否检查了收件人电子邮件Textbox 并检查数据属性中的错误消息是否与Model 验证规则中提到的相同?是否有任何Js 正在运行以显示自定义错误消息?
  • 已编辑问题并添加了发出的 HTML。

标签: validation asp.net-mvc-4 data-annotations


【解决方案1】:

我最终混合使用了 System.ComponentModel.DataAnnotations 和 DataAnnotationsExtensions。我发现大多数时候数据注释也会进行客户端验证。唯一没有客户端验证的情况是,当我检查电话号码的长度是否正确时。

public class NpRequest
{
    [DisplayName("Phone number")]
    [MinLengthAttribute(8, ErrorMessage = "Phone number must be 8 digits")]
    [MaxLengthAttribute(8, ErrorMessage = "Phone number must be 8 digits")]
    [DigitsAttribute(ErrorMessage = "Phone number must be 8 digits")]
    [Required(ErrorMessage = "Phone number is required")]
    public string PhoneNumber { get; set; }

    [DisplayName("Modtagers email adresse")]
    [EmailAddressAttribute(ErrorMessage = "Invalid email")]
    [Required(ErrorMessage = "Email is required")]
    public string RecipientEmail { get; set; }
    public RecipientTypeEnum RecipientType { get; set; }
}

【讨论】:

    猜你喜欢
    • 2015-04-10
    • 2019-12-14
    • 2012-05-08
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-10-02
    • 1970-01-01
    • 2014-03-05
    相关资源
    最近更新 更多