everyone! I'm confusing with implementing a piece of code to make work .net data annotation in asp.net mvc 3 with model with different required fields in several cases (6). I have a model:
public class OpportunityModel
{
public Guid OpportunityId { get; set; }
[Display(Name = "Value")]
[RegularExpression(@"^[-+]?\d{1,10}(\.\d{0,4})?$", ErrorMessage = "Must be a number")]
public decimal? ActualValue { get; set; }
[Display(Name = "Name")]
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
public string Product { get; set; }
[Display(Name = "Estimated Date")]
public DateTime? EstimateDate { get; set; }
public bool? Sales6ixFallDown { get; set; }
[Display(Name = "Stage")]
public Stages Sales6ixStage { get; set; }
public DateTime? Sales6ixDateInBoard { get; set; }
public DateTime? Sales6ixDateInCurrentStage { get; set; }
public DateTime? Sales6ixNextAppointmentDate { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
public string Sales6ixNextAppointmentDescription { get; set; }
public int NewColumn { get; set; }
public Guid? CustomerId { get; set; }
public string CustomerName { get; set; }
}
What I need is the possibility dynamically change required fiefs in it. After some googling that's impossible and came to idea to use model inheritance. I mean: I have a base model like this:
public class BaseOpportunityModel
{
public Guid OpportunityId { get; set; }
public virtual decimal? ActualValue { get; set; }
public virtual string Name { get; set; }
public string Product { get; set; }
public DateTime? EstimateDate { get; set; }
public bool? Sales6ixFallDown { get; set; }
[Display(Name = "Stage")]
public Stages Sales6ixStage { get; set; }
public DateTime? Sales6ixDateInBoard { get; set; }
public DateTime? Sales6ixDateInCurrentStage { get; set; }
public DateTime? Sales6ixNextAppointmentDate { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
public string Sales6ixNextAppointmentDescription { get; set; }
public int NewColumn { get; set; }
public Guid? CustomerId { get; set; }
public string CustomerName { get; set; }
}
where virtual properties are properties that may be or not a required fields. And then I have several derived model from base like this one:
public class OpportunityModel0: BaseOpportunityModel
{
[Display(Name = "Value")]
[Required(ErrorMessage = "Name is required")]
[RegularExpression(@"^[-+]?\d{1,10}(\.\d{0,4})?$", ErrorMessage = "Must be a number")]
public override decimal? ActualValue { get; set; }
[Display(Name = "Name")]
[Required(ErrorMessage = "Name is required")]
public override string Name { get; set; }
}
And then I be able to use in View and Controller base model BaseOpportunityModel. But I encountered follow problem:
- Validation use annotation attributes from BaseOpportunityModel and ignore attributes in derived models.
What do I wrong? Can somebody steer me in the right direction or help me with this issue? Thanks in advance.