Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.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/3/clojure/3.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
C# 按对象实例MVC4的动态数据注释_C#_Asp.net Mvc 4 - Fatal编程技术网

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

C# 按对象实例MVC4的动态数据注释,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我有下面一个名为QuoteDimensions的类(我为了发布一个问题而精简了它)。我想在基于eid的值创建对象时设置高度和宽度的范围。我创建了一个两个自定义的range属性类,从数据库中获取所需的mins和max。此解决方案不起作用,因为我无法(或不知道如何)将运行时变量的值强制转换为常量 using System; using System.Collections.Generic; using System.ComponentModel; using Syst

我有下面一个名为QuoteDimensions的类(我为了发布一个问题而精简了它)。我想在基于eid的值创建对象时设置高度和宽度的范围。我创建了一个两个自定义的range属性类,从数据库中获取所需的mins和max。此解决方案不起作用,因为我无法(或不知道如何)将运行时变量的值强制转换为常量

    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;
    }

}
我考虑过创建一个自定义MetadataProvider,但我认为这不能解决我的问题

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


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

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

我想说,对您来说,最简单的方法是将
Eid
设置为一个字段:

并在
QuoteDimension
类中实现
IValidatableObject

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

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        // check for conditions here
        yield return new ValidationResult("Validation message.");
    }
}
公共类TemplateNameModel:IValidatableObject
{
//类的定义
公共IEnumerable验证(ValidationContext ValidationContext)
{
//检查这里的情况
返回新的ValidationResult(“验证消息”);
}
}

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

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

我想说,对您来说,最简单的方法是将
Eid
设置为一个字段:

并在
QuoteDimension
类中实现
IValidatableObject

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

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        // check for conditions here
        yield return new ValidationResult("Validation message.");
    }
}
公共类TemplateNameModel:IValidatableObject
{
//类的定义
公共IEnumerable验证(ValidationContext ValidationContext)
{
//检查这里的情况
返回新的ValidationResult(“验证消息”);
}
}

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

属性参数必须是常量,因此这似乎没有帮助,除非我不理解您的意思。@Calidus,抱歉,我没有正确地表达自己的意思。请参阅更新后的帖子。谢谢。这更有意义。属性参数必须是常量,因此除非我没有理解您,否则这似乎没有帮助。@Calidus,抱歉,我没有正确地表达自己。请看更新后的帖子。谢谢。这更有意义。