Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
1. I have created Test Class which contain Static Class and Property.

C#
namespace QSys.Data.Domain.DataSecurity
{
    public static class TestData
    {
        public static string MyName { get; set; }
    }
}


2. Customer Model class and Custom Validation

C#
namespace QSys.Data.Domain
{
    [Serializable()]
    public class Customer
    {
        [Key]
        public virtual int Id { get; set; }
        [CustomValidation(typeof(CustomerRequiredRules), "IsCompanyNameEmpty")]
        public virtual string CompanyName { get; set; }
        public virtual string City { get; set; }
    }

    public class CustomerRequiredRules
    {
        public static ValidationResult IsCompanyNameEmpty(string CompanyName, ValidationContext context)
        {
            if (TestData.MyName == "Imdadhusen")
            {
                return new ValidationResult("Company name not allowed!", new string[] { "CompanyName" });
            }
            return ValidationResult.Success;
        }
    }
}


3. Setting value of Static class like

C#
public class AdminHomeViewModel 
{
   public AdminHomeViewModel()
   {
        TestData.MyName = "Imdadhusen";
   }
}


4. I click on submit button, my custom validation getting fired and here i couldn't able to get value of
C#
TestData.MyName
. it will display Null instead of Imdadhusen.


Any Answer, Suggestion or Comment highly appreciated!

Thanks,
Imdadhusen
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900