0

我在 MVC4 中使用实体框架,并且我有以下域类。

我的问题是,如何将 SalePrice 的值限制为 <= Price?我一直在寻找可以做到这一点的合适属性,但还没有找到。

编辑:

假设某人在准备提交表单时输入了 20.00 美元的价格值,然后他们输入了 25.00 美元的销售价格。该程序将不允许这样做,因为销售价格不能高于商品的价格。我想知道是否有[Attribute]可以对 SalePrice 执行此限制的方法。

public class MedicalProduct
{
    [Key]
    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Currency)]
    public double Price { get; set; }

    // needs to be less than price.
    [Required]
    [DataType(DataType.Currency)]
    public double SalePrice { get; set; }

    // is a foreign key
    public int BrandID { get; set; }

}
4

2 回答 2

2

我会创建一个自定义ValidationAttribute来完全满足您的要求。该属性需要一个要比较的属性的名称,以及一个ComparisonType. 我在下面创建了一个非常快速的示例:

比较属性

public enum ComparisonType
{
    LessThan,
    LessThanOrEqual,
    Equal,
    GreaterThanOrEqual,
    GreaterThan,
    NotEqual
}

public sealed class ComparisonAttribute : ValidationAttribute
{

    string PropertyToCompare { get; set; }
    ComparisonType Type { get; set; }

    public ComparisonAttribute(string propertyToCompare, ComparisonType type)
    {
        PropertyToCompare = propertyToCompare;
        Type = type;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (validationContext.ObjectInstance == null || value == null)
            return new ValidationResult("Cannot compare null values");

        PropertyInfo property = validationContext.ObjectType.GetProperty(PropertyToCompare);
        object propertyValue = property.GetValue(validationContext.ObjectInstance, null);

        string errorMessage = "";

        if (value is IComparable)
        {
            int compVal = ((IComparable)value).CompareTo(propertyValue);
            switch (Type)
            {
                case ComparisonType.LessThan:
                    errorMessage = compVal < 0 ? "" : string.Format("{0} is not less than {1}", validationContext.DisplayName, property.Name);
                    break;
                case ComparisonType.LessThanOrEqual:
                    errorMessage = compVal <= 0 ? "" : string.Format("{0} is not less than or equal to {1}", validationContext.DisplayName, property.Name);
                    break;
                case ComparisonType.Equal:
                    errorMessage = compVal == 0 ? "" : string.Format("{0} is not equal to {1}", validationContext.DisplayName, property.Name);
                    break;
                case ComparisonType.GreaterThanOrEqual:
                    errorMessage = compVal >= 0 ? "" : string.Format("{0} is not greater than or equal to {1}", validationContext.DisplayName, property.Name);
                    break;
                case ComparisonType.GreaterThan:
                    errorMessage = compVal > 0 ? "" : string.Format("{0} is not greater than {1}", validationContext.DisplayName, property.Name);
                    break;
                case ComparisonType.NotEqual:
                    errorMessage = compVal != 0 ? "" : string.Format("{0} cannot be equal to {1}", validationContext.DisplayName, property.Name);
                    break;
                default:
                    errorMessage = "";
                    break;
            }
        }

        if (String.IsNullOrEmpty(errorMessage))
            return ValidationResult.Success;

        return new ValidationResult(errorMessage);
    }

}

用法

public class Model
{
    [Comparison("Value2", ComparisonType.LessThanOrEqual)]
    public int Value1 { get; set; }

    public int Value2 { get; set; }

}
于 2013-10-30T02:37:49.593 回答
1

您可以考虑public double SalePrice { get; set; }使用支持字段制作功能更全面的属性。然后,您将测试Pricebefore allowed SalePriceto be的值set。这是一个简单的例子:

private double _salePrice;

public double SalePrice
{
    get { return _salePrice; }
    set
    {
        // only set _salePrice if it's less than
        // or equal to Price
        if(value <= Price)
            _salePrice = value;
    }
}

试试看...

[编辑] - 我看到你稍微改变了这个问题,所以补充一下,CompareAttribute可以用来验证两个属性的相互关系(但是,我确定这仅限于字符串值 - 将确认) . 另外,看看这个页面,它有一个自定义的解决方案,看起来不错而且很灵活,可能对你有用:http ://forums.asp.net/t/1924941.aspx 。gorra head - 00:43 在英国!!

于 2013-10-30T00:30:49.797 回答