Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 针对整个模型的自定义验证属性测试_Asp.net Mvc_Data Annotations_Validationattribute - Fatal编程技术网

Asp.net mvc 针对整个模型的自定义验证属性测试

Asp.net mvc 针对整个模型的自定义验证属性测试,asp.net-mvc,data-annotations,validationattribute,Asp.net Mvc,Data Annotations,Validationattribute,我知道这可能是不可能的,但假设我有一个具有两个属性的模型 我为其中一个属性编写ValidationAttribute。VA能看看其他房产并做出决定吗 所以, 因此,在上面的示例中,验证程序能否测试“state”属性中的内容,并在验证“familyType”时将其考虑在内 我知道我可能可以将对象保存到会话中,但如果可能,我希望避免保存任何状态。实现此类验证的另一种方法是让您的模型实现IDataErrorInfo。这样,您就可以执行整个viewmodel验证 有一些关于我实现IDataErrorIn

我知道这可能是不可能的,但假设我有一个具有两个属性的模型

我为其中一个属性编写ValidationAttribute。VA能看看其他房产并做出决定吗

所以,

因此,在上面的示例中,验证程序能否测试“state”属性中的内容,并在验证“familyType”时将其考虑在内


我知道我可能可以将对象保存到会话中,但如果可能,我希望避免保存任何状态。

实现此类验证的另一种方法是让您的模型实现IDataErrorInfo。这样,您就可以执行整个viewmodel验证


有一些关于我实现IDataErrorInfo接口的信息,大约是“实现IDataErrorInfo接口”标题下的2/3。实现这种验证的另一种方法是让您的模型实现IDataErrorInfo。这样,您就可以执行整个viewmodel验证


有一些关于我实现IDataErrorInfo接口的信息,大约是“实现IDataErrorInfo接口”标题下的2/3,查看AccountModels类中的PropertiesMustMatch属性,该属性默认情况下作为VS2008中MVC项目模板的一部分创建。

您的自定义验证可以直接应用于该类,查看AccountModels类中的PropertiesMustMatch属性,该属性默认情况下作为VS2008中MVC项目模板的一部分创建。

使用ValidationContext获取模型:

 public class MyRequiredValidator: RequiredAttribute
    {
        public override bool RequiresValidationContext
        {
            get {return true;} //it needs another propertie in model            
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            QuickQuote model = (QuickQuote)validationContext.ObjectInstance;

            if (model.state == "single")
                return null;
            else
                return base.IsValid(value, validationContext);//familyType is require for married
        }      
    }

使用ValidationContext获取模型:

 public class MyRequiredValidator: RequiredAttribute
    {
        public override bool RequiresValidationContext
        {
            get {return true;} //it needs another propertie in model            
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            QuickQuote model = (QuickQuote)validationContext.ObjectInstance;

            if (model.state == "single")
                return null;
            else
                return base.IsValid(value, validationContext);//familyType is require for married
        }      
    }

+这可能就是我要找的。现在读吧,谢谢。+1,这可能就是我要找的。现在读吧,谢谢。