数据注释和元数据类型以及“将值拼接在一起”

发布于 2024-09-28 06:11:10 字数 858 浏览 4 评论 0原文

我正在尝试为模型中的对象设置数据注释验证,并且它有特殊需要。我本质上必须采用一些我添加的额外属性并将它们组合起来以获得不动产的价值。例如,我有一个基本对象:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月蹉跎了容颜 2024-10-05 06:11:10

你只需要覆盖你的额外属性的获取/设置:

public string DueDate {
  get {return Due.Date.ToShortDateString();}
  set { Due.Date = DateTime.Parse(value); }
}
public string DueTimeHour{
  get {return Due.Hours}
  set { Due.Hours = Int32.Parse(value); }
}
...

或者我错过了一些东西?

You just have to override the get/set of your extra properties:

public string DueDate {
  get {return Due.Date.ToShortDateString();}
  set { Due.Date = DateTime.Parse(value); }
}
public string DueTimeHour{
  get {return Due.Hours}
  set { Due.Hours = Int32.Parse(value); }
}
...

Or i missed something ?

没有你我更好 2024-10-05 06:11:10

所以,我只花了几个小时的睡眠就解决了这个问题。这是我的最终代码版本,适用于将来可能遇到相同问题的任何人:

public partial class Task {
    public DateTime DueDate {
        get {
            return (DateTime.Now);
        }
        set {
            this.Due = value;
        }
    }

    [Range(0, 23)]
    public byte DueTimeHour {
        get {
            return (0);
        }
        set {
            if (this.Due.HasValue) {
                this.Due = DateTime.Parse(this.Due.Value.ToShortDateString() + " " + value + ":00:00");
            };
        }
    }

    [Range(0, 59)]
    public byte DueTimeMinute {
        get {
            return (0);
        }
        set {
            if (this.Due.HasValue) {
                this.Due = DateTime.Parse(this.Due.Value.ToString("M/d/yyyy h") + ":" + value + ":00");
            };
        }
    }

    public string DueTimePostfix {
        get {
            return (string.Empty);
        }
        set {
            if (this.Due.HasValue) {
                this.Due = DateTime.Parse(this.Due.Value.ToString("M/d/yyyy h:m") + " " + value);
            };
        }
    }
}

最终,我必须添加一个 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:

public partial class Task {
    public DateTime DueDate {
        get {
            return (DateTime.Now);
        }
        set {
            this.Due = value;
        }
    }

    [Range(0, 23)]
    public byte DueTimeHour {
        get {
            return (0);
        }
        set {
            if (this.Due.HasValue) {
                this.Due = DateTime.Parse(this.Due.Value.ToShortDateString() + " " + value + ":00:00");
            };
        }
    }

    [Range(0, 59)]
    public byte DueTimeMinute {
        get {
            return (0);
        }
        set {
            if (this.Due.HasValue) {
                this.Due = DateTime.Parse(this.Due.Value.ToString("M/d/yyyy h") + ":" + value + ":00");
            };
        }
    }

    public string DueTimePostfix {
        get {
            return (string.Empty);
        }
        set {
            if (this.Due.HasValue) {
                this.Due = DateTime.Parse(this.Due.Value.ToString("M/d/yyyy h:m") + " " + value);
            };
        }
    }
}

Ultimately, I had to add a get to make it work, which makes me curious. I was hoping to get by with just the set on each property because I was already manipulating the Due value in there, but it wouldn't work without the get. Maybe someone can explain that one to me?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文