覆盖 ComponentModel 属性(即 DisplayName)无法按预期工作

发布于 2024-09-17 01:31:08 字数 770 浏览 10 评论 0原文

我有两个类:

public class DocumentViewModel
{
    public virtual string DocumentNumber { get; set; }
}

public class PurchaseOrderViewModel : DocumentViewModel
{
    [DisplayName("PO Number")]
    public override string DocumentNumber { get; set; }
}

和一个视图:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Onyx.Web.Models.PurchaseOrderViewModel>" %>

<strong><%: Html.LabelFor(i => i.DocumentNumber) %>:</strong> <%: Model.DocumentNumber %>

我希望它能够渲染,

<strong>PO Number:</strong> PO-12345

但它实际上渲染了

<strong>DocumentNumber:</strong> PO-12345

有没有办法解决这个问题?

I have two classes:

public class DocumentViewModel
{
    public virtual string DocumentNumber { get; set; }
}

public class PurchaseOrderViewModel : DocumentViewModel
{
    [DisplayName("PO Number")]
    public override string DocumentNumber { get; set; }
}

And a view:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Onyx.Web.Models.PurchaseOrderViewModel>" %>

<strong><%: Html.LabelFor(i => i.DocumentNumber) %>:</strong> <%: Model.DocumentNumber %>

I expect that to render

<strong>PO Number:</strong> PO-12345

but it actually renders

<strong>DocumentNumber:</strong> PO-12345

Is there a way to get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

执手闯天涯 2024-09-24 01:31:08

我已经想出了解决我自己问题的方法。它并不完美,但还可以。

由于这些只是 ViewModel,因此其中没有逻辑。因此,我将class DocumentViewModel 更改为interface IDocumentViewModel,然后问题就解决了。

我仍然想让它适用于继承类,但这更多的是我的固执而不是任何商业案例。

I've come up with a solution to my own problem. It's not perfect, but it's alright.

Since these are just ViewModels, there's not logic in them. So, I changed class DocumentViewModel to interface IDocumentViewModel and, voilà, problem solved.

I'd still like to get this working for inheriting classes, but that's more my stubbornness than any business case.

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