문제

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