c# - 令人困惑的数据注释验证

标签 c# silverlight validation class data-annotations

从早上开始我就一直在为这个问题苦苦挣扎。我的域服务中有以下类。

 [MetadataTypeAttribute(typeof(CompanyRecord.CompanyRecordMetadata))]
 public partial class CompanyRecord
 {

     // This class allows you to attach custom attributes to properties
     // of the CompanyRecord class.
     //
     // For example, the following marks the Xyz property as a
     // required property and specifies the format for valid values:
     //    [Required]
     //    [RegularExpression("[A-Z][A-Za-z0-9]*")]
     //    [StringLength(32)]
     //    public string Xyz { get; set; }
    internal sealed class CompanyRecordMetadata
    {
        // Metadata classes are not meant to be instantiated.
        private CompanyRecordMetadata()
        {
        }

        public Nullable<char> AccessToClearance { get; set; }

        public Nullable<char> AccessToMarketplace { get; set; }

        public Nullable<bool> Active { get; set; }

        public string AddressLine1 { get; set; }

        public string AddressLine2 { get; set; }

        public Nullable<int> AllotedHDSpace { get; set; }

        public string City { get; set; }

        public int CompanyID { get; set; }

        public Binary CompanyLogo { get; set; }

        [Required(ErrorMessage = "Company Name is required and must be unique.")]
        public string CompanyName { get; set; }

        [Range(1, Int32.MaxValue, ErrorMessage = "Company Type is required.")]
        public int CompanyTypeID { get; set; }

        [Range(1, Int32.MaxValue, ErrorMessage = "Country is required.")]
        public Nullable<int> CountryID { get; set; }

        public string Description { get; set; }

        //[RegularExpression(pattern: @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|tv|COM|ORG|NET|GOV|MIL|BIZ|INFO|MOBI|NAME|AERO|JOBS|MUSEUM|TV|Com|Org|Net|Gov|Mil|Biz|Info|Mobi|Name|Aero|Jobs|Museum|Tv)\b", ErrorMessage = "Please provide a valid email address")]
        [RegularExpression(pattern: @"(\w[-._\w]*\w@\w[-._\w]*\w\.[a-zA-z]{2,6})", ErrorMessage = "Please provide a valid email address")]
        public string EmployeeEmail { get; set; }

        //[RegularExpression(pattern: @"[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|tv|COM|ORG|NET|GOV|MIL|BIZ|INFO|MOBI|NAME|AERO|JOBS|MUSEUM|TV|Com|Org|Net|Gov|Mil|Biz|Info|Mobi|Name|Aero|Jobs|Museum|Tv)\b", ErrorMessage = "Please provide a valid email address")]
        //(\w[-._\w]*\w@\w[-._\w]*\w\.\w{2,3})
        [RegularExpression(pattern: @"(\w[-._\w]*\w@\w[-._\w]*\w\.[a-zA-z]{2,6})", ErrorMessage = "Please provide a valid email address")]
        [Required(ErrorMessage = "Email is required.")]
        public string Email { get; set; }

        [StringLength(10, ErrorMessage = "Phone Ext. should not be more than 10 chars.")]
        public string EmployeePhoneExt { get; set; }

        [StringLength(20, ErrorMessage = "Phone No. should not be more than 20 chars.")]
        [RegularExpression(pattern: @"^[0-9\-\(\) ]*[0-9\-\(\)]*$", ErrorMessage = "Please provide a valid Phone No.")]
        public string EmployeePhoneNo { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        [Range(0, int.MaxValue, ErrorMessage = "Number of projects should be lower than 2,147,483,647.")]
        public Nullable<int> NoOfProjects { get; set; }

        public Nullable<int> NoOfUsers { get; set; }

        [StringLength(10, ErrorMessage = "Phone Ext. should not be more than 10 chars.")]
        public string PhoneExt { get; set; }

        [StringLength(20, ErrorMessage = "Phone No. should not be more than 20 chars.")]
        [Required(ErrorMessage = "Phone No. is required.")]
        [RegularExpression(pattern: @"^[0-9\-\(\) ]*[0-9\-\(\)]*$", ErrorMessage = "Please provide a valid Phone No.")]
        public string PhoneNo { get; set; }

        [RegularExpression(pattern: @"^[a-zA-Z0-9\-\(\)\* ]*[a-zA-Z0-9\-\(\)\*]*$", ErrorMessage = "Postal codes cant' contain special characters except for -, (, ), * and spaces.")]            
        public string PostalCode { get; set; }

        public string Province { get; set; }

        public string Website { get; set; }

        public int MarketID { get; set; }

        public int AffiliationID { get; set; }

        public string CallLetter { get; set; }
    }
}

问题:我需要创建一个验证规则,仅当 CompanyTypeID 等于 1、3、5、9 和 11 时才需要 MarketID 和附属 ID 是否有人知道如何解决这个问题?

最佳答案

关于c# - 令人困惑的数据注释验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10511609/

相关文章:

c# - 使用 WPF 和 SlimDx (DirectX 10/11)

c# - 让子对象继承自特定的父对象

c# - 对于 ASP.NET Core 3.0 终结点,什么相当于 `MapSpaFallbackRoute`?

c# - TextBox.Text += "string";与 TextBox.AppendText ("string");

wpf - IValueConverter.Convert 从哪里获取 CultureInfo 参数?

silverlight - 如何将 Silverlight DatePicker 控件设置为只读?

javascript - 具有多个提交按钮的条件表单验证

validation - 如何在自定义验证器中获取另一个组件的值?

Java如何验证2个输入是否为 float