c# - Dynamic Data Annotations by Object Instance MVC4 -


i have following class named quotedimensions(i slimmed down posting question). set range of height , width when object created based on valve of eid. made 2 custom range attribute class desired mins , maxs database. solution doesn't work because can't(or don't know how to) cast value of run time variable constant.

    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 way         public quotedimension(int eid)         {            eid= eid; ///constants don't work 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                        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                        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                        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                        ed.enclosure_id_fk == eid                        select ed.heightmin).single();         return temp;     }  } 

i looked creating custom metadataprovidor don't think solve problem.

since can't seem way working other idea create quotedimensions interface, create multiple classes implement interface , hard code range in each class. way kinds stinks because can't change a max or min in database effect website.

any thoughts or suggestions every helpful. thank you.

constants cannot changed @ runtime. in fact value resolved , every occurrence of constant substituted value during compilation process. why re trying impossible.

i easiest way here make eid readonly field:

private readonly int eid;  public quotedimension(int eid) {    eid = eid; }  public quotedimension(int eid) : this(0) { } 

and implement ivalidatableobject in quotedimension class:

public class templatenamemodel : ivalidatableobject {     //definition of class      public ienumerable<validationresult> validate(validationcontext validationcontext)     {         // check conditions here         yield return new validationresult("validation message.");     } } 

this might require refactoring of custom attributes other form of validators, allow change parameters @ runtime.


Comments