ASP中的动态范围验证。NET MVC 2

时间:2022-10-28 16:39:09

I am using ASP.NET MVC2 and trying to validate my view models using the attributes in System.ComponentModel.DataAnnotations namespace.

我用ASP。使用System.ComponentModel中的属性来验证我的视图模型。DataAnnotations名称空间。

How can I dynamically set the permitted valid range of a RangeAttribute? For example, if I want to validate that a date entered is within an expected range.

如何动态设置允许的RangeAttribute有效范围?例如,如果我想验证输入的日期是否在预期范围内。

This doesn't compile:

这并不编译:

[Range(typeof(DateTime), 
        DateTime.Today.ToShortDateString(), 
        DateTime.Today.AddYears(1).ToShortDateString())]
    public DateTime DeliveryDate { get; set; }

because "an attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type".

因为“属性参数必须是属性参数类型的常量表达式、类型表达式或数组创建表达式”。

Do I need to resort to creating my own custom validator?

我需要使用创建自己的定制验证器吗?

2 个解决方案

#1


15  

OK, found the answer. .NET Framework 4 provides a new CustomValidationAttribute which makes the following possible:

. net Framework 4提供了一个新的CustomValidationAttribute,它使下列成为可能:

[Required]
[DisplayName("Ideal Delivery Date")]
[CustomValidation(typeof(HeaderViewModel), "ValidateDeliveryDate")]
public DateTime DeliveryDate { get; set; }

public static ValidationResult ValidateDeliveryDate(DateTime deliveryDateToValidate)
{
    if (deliveryDateToValidate.Date < DateTime.Today)
    {
    return new ValidationResult("Delivery Date cannot be in the past.");
    }

    if (deliveryDateToValidate.Date > DateTime.Today.AddYears(1))
    {
    return new ValidationResult("Delivery Date must be within the next year.");
    }

    return ValidationResult.Success;
}

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute%28VS.100%29.aspx

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute%28VS.100%29.aspx

#2


0  

You need to create your own attribute or use a none attribute based validation framework. As the message say, all parameters to any attribute needs to be constant values.

您需要创建自己的属性,或者使用基于none属性的验证框架。如消息所说,任何属性的所有参数都必须是常量值。

#1


15  

OK, found the answer. .NET Framework 4 provides a new CustomValidationAttribute which makes the following possible:

. net Framework 4提供了一个新的CustomValidationAttribute,它使下列成为可能:

[Required]
[DisplayName("Ideal Delivery Date")]
[CustomValidation(typeof(HeaderViewModel), "ValidateDeliveryDate")]
public DateTime DeliveryDate { get; set; }

public static ValidationResult ValidateDeliveryDate(DateTime deliveryDateToValidate)
{
    if (deliveryDateToValidate.Date < DateTime.Today)
    {
    return new ValidationResult("Delivery Date cannot be in the past.");
    }

    if (deliveryDateToValidate.Date > DateTime.Today.AddYears(1))
    {
    return new ValidationResult("Delivery Date must be within the next year.");
    }

    return ValidationResult.Success;
}

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute%28VS.100%29.aspx

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute%28VS.100%29.aspx

#2


0  

You need to create your own attribute or use a none attribute based validation framework. As the message say, all parameters to any attribute needs to be constant values.

您需要创建自己的属性,或者使用基于none属性的验证框架。如消息所说,任何属性的所有参数都必须是常量值。