I’m trying to setup data annotation validation for an object in my model and it has a special need. I have to essentially take some extra properties that I’m adding and combine them to get the value of a real property. For example I have a base object:
Task {
public DateTime? Due
}
But my form has the fields of:
Task.DueDate
Task.DueTimeHour
Task.DueTimeMinute
Task.DueTimePostfix
So I tried:
Task {
public string DueDate
public string DueTimeHour
public string DueTimeMinute
public string DueTimePostfix
}
Which works, but I then want the model to automatically populate the value of Due with the parsed value of all the other ones. I don’t know how to do that.
I would have to override the getter/setters of Due in the metadata buddy class, but that class wouldn’t be aware of the extra properties I’ve given to the original class. Plus I have a feeling that if I mess with those I will have problems retrieving real values from the database.
So I’m hoping that someone here may have an idea on how I can accomplish what I want. If I’m going about it completely wrong I would appreciate being guided back on the right path.
Thanks in advance for any suggestions!
UPDATE
So, I might have found a solution for the first part by having both the original class and the metadata buddy class extend from another base class which contains all the properties. Thus both classes see the properties and can use them.
Now, I’m having a hard time setting the value for the Due property based off of the other properties. Help would be appreciated on this front.
So, I figured it out at the expense of only a couple of hours of sleep. Here’s my final code version that works for anyone who may have the same issues in the future:
Ultimately, I had to add a
getto make it work, which makes me curious. I was hoping to get by with just theseton each property because I was already manipulating theDuevalue in there, but it wouldn’t work without theget. Maybe someone can explain that one to me?