Static Class Property getting NULL when Custom Val

2019-09-11 05:08发布

问题:

1. I have created Test Class which contain Static Class and Property.

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

2. Customer Model class and Custom Validation

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

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 TestData.MyName. it will display Null instead of Imdadhusen.

Any Answer, Suggestion or Comment highly appreciated!

Thanks, Imdadhusen