Вопрос

I have MVC 5 project that uses unobtrusive validation:

<appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

And here is my model class:

public class LoginModel {
    [DataType(DataType.EmailAddress, ErrorMessage = "Problem address")]
    [Required]
    public string Mail { get; set; }

    [DataType(DataType.Password)]
    [Required]
    public string Password { get; set; }
}

When I enter invalid value in Mail field I always get standard "Please enter a valid email address." which is hard-coded in jquery.validate.js. My ErrorMessage value is ignored. Why? Moreover, if I remove ErrorMessage parameter the standard message is not localized unlike the rest of validation attributes (Required, StringLength). What am I missing?

Это было полезно?

Решение

The purpose of the DataType attribute is not validation, it is used for formatting data in views, choosing which type of input field to generate etc.

The error message comes from the validation attribute. Try:

[EmailAddress(ErrorMessage = "Problem address")]
public string Email{ get; set; }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top