标签: data-annotations

DataAnnotations似乎无法使用Validator在值类型上正常运行

为什么这总是回归真的?

 class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.Age = 24;

            ICollection<ValidationResult> results = new Collection<ValidationResult>();
            bool isValid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), results);

            Console.WriteLine("Valid = {0}",isValid);

            foreach (var result in results)
            {
                Console.WriteLine(result.ErrorMessage);
            }

            Console.ReadKey();
        }
    }

    public class Person
    {
        [Required(ErrorMessage = "You have to identify yourself!!")]
        public int Id { get; set; }

        public decimal Age { get; set; }    

    }
Run Code Online (Sandbox Code Playgroud)

我的用法有什么问题?

c# data-annotations

0
推荐指数
1
解决办法
205
查看次数

数据注释,IDataErrorInfo和MVVM

我正在尝试找到验证MVVM中数据的最佳方法.目前,我正在尝试使用MVVM模式将IDataErrorInfo与Data Annotations一起使用.

然而,似乎没有任何工作,我不知道我可能做错了什么.我有类似的东西.

模型

public class Person : IDataErrorInfo
{
    [Required(ErrorMessage="Please enter your name")]
    public string Name { get; set; }

    public string Error
    {
        get { throw new NotImplementedException(); }
    }

    string IDataErrorInfo.this[string propertyName]
    {
        get
        {
            return OnValidate(propertyName);
        }
    }

    protected virtual string OnValidate(string propertyName)
    {
        if (string.IsNullOrEmpty(propertyName))
            throw new ArgumentException("Property may not be null or empty", propertyName);

        string error = string.Empty;

        var value = this.GetType().GetProperty(propertyName).GetValue(this, null);
        var results = new List<ValidationResult>();

        var context = new ValidationContext(this, null, …
Run Code Online (Sandbox Code Playgroud)

c# validation wpf mvvm data-annotations

0
推荐指数
1
解决办法
5211
查看次数

如何从 ServiceStack ORMLite 检索数据注释别名(“表名”)和别名(“字段名”)?

我需要从此类“tblAccounts”和“AccountNumber”的数据注释中返回别名。它使用 ServiceStack ORM Lite。

[Alias("tblAccounts")]
    [Schema("task")]
    public class Account : IHasId<int>
    {
        [Alias("AccountNumber")]
        public int Id { get; set; }
        [Required]
        public int UnitId { get; set; }
        [Required]
        public int OldAccountNumber { get; set; }
        [Required]
    }
Run Code Online (Sandbox Code Playgroud)

c# data-annotations servicestack ormlite-servicestack

0
推荐指数
1
解决办法
2160
查看次数

如何在 C# 中使用 EmailAddressAttribute 验证字符串列表?

我有一堂课说“请求”。它有两个属性,如下所示。收件人的电子邮件验证工作正常。但是,它不适用于收件人。

[EmailAddress] 
public string Recipient { get; set; }

[EmailAddress] 
public List<string> Recipients { get; set; }
Run Code Online (Sandbox Code Playgroud)

感谢任何帮助。

c# validation email-address data-annotations

0
推荐指数
1
解决办法
1285
查看次数