Validator.TryValidateProperty 抛出 ArgumentException

我使用 ListValidation


public class Test

    {


        [ListValidation(ErrorMessage ="wrong")]

        public List<string> Listt { get; set; }

    }

ListValidation 实现


[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]

    public class ListValidationAttribute : ValidationAttribute

    {

        public override bool IsValid(object value)

        {

            var list = value as IList;

            if (list != null)

            {

                return list.Count > 0;

            }

            return false;

        }

    }

当我测试它时


Test t = new Test();

            List<string> str = new List<string>();

            str.Add("haha");

            str.Add("hoho");


            t.Listt = str;


            JsonResult json = ModelValidation.ValidateProperty(t, nameof(t.Listt));

它抛出 ArgumentException


{System.ArgumentException: The value for property 'Listt' must be of type 'System.Collections.Generic.List`1[System.String]'.

Parameter name: value

   at System.ComponentModel.DataAnnotations.Validator.EnsureValidPropertyType(String propertyName, Type propertyType, Object value)

   at System.ComponentModel.DataAnnotations.Validator.TryValidateProperty(Object value, ValidationContext validationContext, ICollection`1 validationResults)

   at EArchive.Infrastructure.ModelValidation.ValidateProperty(Object obj, String property) in C:\Users\haha\ModelValidation.cs:line 54}


这里有什么问题?


慕神8447489
浏览 225回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP