Either ErrorMessageString or ErrorMessageResourceName must be set, but not both error using CreditCardAttribute

前端 未结 8 1955
囚心锁ツ
囚心锁ツ 2020-12-24 04:44

This is my model:

namespace MvcApplication2.Models
{
    public class CreditCard
    {
        [CreditCard(ErrorMessageResourceType = typeof(Messages), Error         


        
8条回答
  •  半阙折子戏
    2020-12-24 05:36

    I had a simmilar issue with a custom ValidationAttribute.

    In the IsValid method I was setting the ErrorMessage. The fix was to remove the assignation to the ErrorMessage propety...

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        //code before ...
        this.ErrorMessage = this.FormatErrorMessage(validationContext.DisplayName); //NOT GOOD...
        ValidationResult validationResult = new ValidationResult(this.ErrorMessage, //And using the ErrorMessage Here...
            new[] { validationContext.MemberName });
        return validationResult;
    }
    

    I was writting Unit test and they were pasisng only if i was running/debugging one by one. But when I click "Run All", only the first one was passing? They are not Linked in any ways...

    So yeah, just remove the remove the assignation to the ErrorMessage propety

    I hope it will help someone!

提交回复
热议问题