对象实例MVC4的动态数据注释

时间:2013-07-11 14:10:55

标签: c# asp.net-mvc-4

我有以下名为QuoteDimensions的类(我在发布问题时将其缩减了)。我想根据eid的阀门设置对象创建时的高度和宽度范围。我制作了两个自定义范围属性类,以从数据库中获取所需的分钟和最大值。这个解决方案不起作用,因为我无法(或者不知道如何)将运行时变量的值转换为常量。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;


   public partial class QuoteDimension
    {
        private const int Eid=0;  ///constants don't work this way
        public QuoteDimension(int eid)
        {
           Eid= eid; ///constants don't work this way
        //dostuff
        }
        [Required]
        public int ID { get; set; }
        [Required]
        public int Quote_ID_FK { get; set; }
        [Required]
        [CustomRangeAttributeHeight(Eid)]
        public Double Height { get; set; }
        [Required]
        [CustomRangeAttributeWidth(Eid)]
        public Double Width { get; set; }


}

public class CustomRangeAttributeWidth : RangeAttribute
{
    public static int Eid;

    public CustomRangeAttributeWidth(int eid)
        : base(getmin(), getmax())
    {
        Eid = eid;
    }
    private static double getmax()
    {
        QuoteDatabaseEntities db = new QuoteDatabaseEntities();
        double temp = (from ed in db.EnclosureDimensions
                       where ed.Enclosure_ID_FK == Eid
                       select ed.WidthMax).Single();

        consta
        return temp;
    }
    private static double getmin()
    {
        QuoteDatabaseEntities db = new QuoteDatabaseEntities();
        double temp = (from ed in db.EnclosureDimensions
                       where ed.Enclosure_ID_FK == Eid
                       select ed.WidthMin).Single();

        return temp;
    }

}
public class CustomRangeAttributeHeight : RangeAttribute
{
    private static int Eid;
    public CustomRangeAttributeHeight(int eid)
        : base(getmin(), getmax())
    {
        Eid = eid;
    }
    private static double getmax()
    {

        QuoteDatabaseEntities db = new QuoteDatabaseEntities();
        double temp = (from ed in db.EnclosureDimensions
                       where ed.Enclosure_ID_FK == Eid
                       select ed.HeightMax).Single();
        return temp;
    }
    private static double getmin()
    {
        QuoteDatabaseEntities db = new QuoteDatabaseEntities();
        double temp = (from ed in db.EnclosureDimensions
                       where ed.Enclosure_ID_FK == Eid
                       select ed.HeightMin).Single();
        return temp;
    }

}

我研究了创建一个自定义metadataprovidor,但我认为这不会解决我的问题。

由于我似乎无法以这种方式工作,我的另一个想法是创建一个QuoteDimensions接口,然后创建多个类来实现接口并硬编码每个类的范围。这种方式很糟糕,因为我不能仅仅改变数据库中的最大值或最小值来影响网站。

任何想法或建议都会有所帮助。谢谢。

1 个答案:

答案 0 :(得分:4)

在运行时无法更改常量。事实上,它们的值已经解析,并且在编译过程中每次出现的常量都会被其值替换。这就是为什么你要做的事情是不可能的。

我想说这里最简单的方法是让Eid成为readonly字段:

private readonly int Eid;

public QuoteDimension(int eid)
{
   Eid = eid;
}

public QuoteDimension(int eid) : this(0)
{
}

并在IValidatableObject课程中实施QuoteDimension

public class TemplateNameModel : IValidatableObject
{
    //definition of the class

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        // check for conditions here
        yield return new ValidationResult("Validation message.");
    }
}

这可能需要将自定义属性重构为其他形式的验证器,但允许您在运行时更改其参数。

相关问题