数据注释和元数据类型以及“将值拼接在一起”
我正在尝试为模型中的对象设置数据注释验证,并且它有特殊需要。我本质上必须采用一些我添加的额外属性并将它们组合起来以获得不动产的价值。例如,我有一个基本对象:
Task {
public DateTime? Due
}
但我的表单具有以下字段:
Task.DueDate
Task.DueTimeHour
Task.DueTimeMinute
Task.DueTimePostfix
所以我尝试了:
Task {
public string DueDate
public string DueTimeHour
public string DueTimeMinute
public string DueTimePostfix
}
哪个有效,但我希望模型自动使用所有解析值来填充 Due
的值其他的。我不知道该怎么做。
我必须重写元数据好友类中 Due
的 getter/setter,但该类不会知道我赋予原始类的额外属性。另外,我有一种感觉,如果我搞乱这些,我将无法从数据库中检索实际值。
所以我希望这里有人可以知道我如何完成我想要的事情。如果我的做法完全错误,我会很高兴能被引导回到正确的道路上。
预先感谢您的任何建议!
更新
因此,我可能已经找到了第一部分的解决方案,即让原始类和元数据伙伴类从另一个包含所有属性的基类扩展。因此,两个类都可以看到这些属性并可以使用它们。
现在,我很难根据其他属性设置 Due
属性的值。在这方面提供帮助将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你只需要覆盖你的额外属性的获取/设置:
或者我错过了一些东西?
You just have to override the get/set of your extra properties:
Or i missed something ?
所以,我只花了几个小时的睡眠就解决了这个问题。这是我的最终代码版本,适用于将来可能遇到相同问题的任何人:
最终,我必须添加一个
get
才能使其工作,这让我很好奇。我希望只使用每个属性上的set
就可以了,因为我已经在其中操纵了Due
值,但如果没有get 它就无法工作
。也许有人可以向我解释一下?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
get
to make it work, which makes me curious. I was hoping to get by with just theset
on each property because I was already manipulating theDue
value in there, but it wouldn't work without theget
. Maybe someone can explain that one to me?