2

私は朝からこの問題に苦しんでいました。ドメインサービスに次のクラスがあります。

 [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と同等である場合にのみ、woudlがMarketIDとAffiliation IDを必要とする検証ルールを作成する必要があります。これを解決する方法についてアイデアを持っている人はいますか?

4

1 に答える 1

1

カスタム検証を使用する必要があります。以下にあなたの質問に対するいくつかのトピックがあります:

1 http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute(vs.95).aspx

2 http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/04/14/net-ria-services-custom-validation.aspx

于 2012-05-09T08:40:36.620 に答える