Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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 2 asp.net mvc2中的条件或验证_Asp.net Mvc 2_Validation - Fatal编程技术网

Asp.net mvc 2 asp.net mvc2中的条件或验证

Asp.net mvc 2 asp.net mvc2中的条件或验证,asp.net-mvc-2,validation,Asp.net Mvc 2,Validation,在我的注册页面中,我有陆路电话号码和手机号码字段 我需要确保用户需要在陆地线路或手机上至少添加一个电话号码 我该怎么做 谢谢 Arnab您可以编写一个自定义验证属性,并用它装饰您的模型: [AttributeUsage(AttributeTargets.Class)] public class AtLeastOnePhoneAttribute: ValidationAttribute { public override bool IsValid(object value) {

在我的注册页面中,我有陆路电话号码和手机号码字段

我需要确保用户需要在陆地线路或手机上至少添加一个电话号码

我该怎么做

谢谢
Arnab

您可以编写一个自定义验证属性,并用它装饰您的模型:

[AttributeUsage(AttributeTargets.Class)]
public class AtLeastOnePhoneAttribute: ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var model = value as SomeViewModel;
        if (model != null)
        {
            return !string.IsNullOrEmpty(model.Phone1) ||
                   !string.IsNullOrEmpty(model.Phone2);
        }
        return false;
    }
}
然后:

[AtLeastOnePhone(ErrorMessage = "Please enter at least one of the two phones")]
public class SomeViewModel
{
    public string Phone1 { get; set; }
    public string Phone2 { get; set; }
}

对于更高级的验证方案,您可以查看或。

您可以编写一个自定义验证属性,并用它装饰您的模型:

[AttributeUsage(AttributeTargets.Class)]
public class AtLeastOnePhoneAttribute: ValidationAttribute
{
    public override bool IsValid(object value)
    {
        var model = value as SomeViewModel;
        if (model != null)
        {
            return !string.IsNullOrEmpty(model.Phone1) ||
                   !string.IsNullOrEmpty(model.Phone2);
        }
        return false;
    }
}
然后:

[AtLeastOnePhone(ErrorMessage = "Please enter at least one of the two phones")]
public class SomeViewModel
{
    public string Phone1 { get; set; }
    public string Phone2 { get; set; }
}

有关更高级的验证方案,您可以查看或。

添加可应用于单个属性的解决方案,而不是在类级别重写验证方法

创建以下自定义属性。注意构造函数中的“otherPropertyName”参数。这将允许您传入要在验证中使用的其他属性

public class OneOrOtherRequiredAttribute: ValidationAttribute
{
    public string OtherPropertyName { get; set; }
    public OneOrOtherRequiredAttribute(string otherPropertyName)
    {
        OtherPropertyName = otherPropertyName;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherPropertyName);
        var otherValue = (string)otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
        if (string.IsNullOrEmpty(otherValue) && string.IsNullOrEmpty((string)value))
        {
            return new ValidationResult(this.ErrorMessage); //The error message passed into the Attribute's constructor
        }
        return null;
    }
}
然后,您可以像这样装饰您的财产:(请务必输入要比较的其他财产的名称)


正在添加可应用于单个属性的解决方案,而不是在类级别重写验证方法

创建以下自定义属性。注意构造函数中的“otherPropertyName”参数。这将允许您传入要在验证中使用的其他属性

public class OneOrOtherRequiredAttribute: ValidationAttribute
{
    public string OtherPropertyName { get; set; }
    public OneOrOtherRequiredAttribute(string otherPropertyName)
    {
        OtherPropertyName = otherPropertyName;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherPropertyName);
        var otherValue = (string)otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
        if (string.IsNullOrEmpty(otherValue) && string.IsNullOrEmpty((string)value))
        {
            return new ValidationResult(this.ErrorMessage); //The error message passed into the Attribute's constructor
        }
        return null;
    }
}
然后,您可以像这样装饰您的财产:(请务必输入要比较的其他财产的名称)


验证正在工作,但没有显示错误消息,显示了针对各个字段添加的所需验证,就像在本例中,我正在向类添加验证注释一样,我需要做些什么吗extra@Arnab,验证消息将与
ValidationSummary
helper一起显示。因为这是一个类级验证程序,所以它与任何属性都没有关联,所以添加到模型状态的键为空。如果要将其与某些字段关联,则需要使用其他框架,如我在回答中链接到的框架,因为数据批注不支持此框架。验证摘要在那里,但不显示。这可能是因为我在模型中使用注释,而模型在视图中调用的viewmodel中。@Arnab,您是这样使用它的:
?+1用于向我指出“万无一失”。我遇到了非此即彼的问题,并用“[RequiredIfEmpty]”属性万无一失地解决了它。验证正在工作,但没有显示错误消息,针对单个字段添加的所需验证正在显示,就像在本例中,我正在向类添加验证注释一样,我需要做些什么吗extra@Arnab,验证消息将与
ValidationSummary
helper一起显示。因为这是一个类级验证程序,所以它与任何属性都没有关联,所以添加到模型状态的键为空。如果要将其与某些字段关联,则需要使用其他框架,如我在回答中链接到的框架,因为数据批注不支持此框架。验证摘要在那里,但不显示。这可能是因为我在模型中使用注释,而模型在视图中调用的viewmodel中。@Arnab,您是这样使用它的:
?+1用于向我指出“万无一失”。我遇到了非此即彼的问题,并且用“[RequiredIfEmpty]”属性万无一失地解决了这个问题。