简体   繁体   English

重写ComponentModel属性(即DisplayName)无法按预期工作

[英]Overridding ComponentModel attributes (ie, DisplayName) doesn't work as expected

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? 有办法解决这个问题吗?

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. 由于这些只是ViewModel,因此其中没有逻辑。 So, I changed class DocumentViewModel to interface IDocumentViewModel and, voilà , problem solved. 所以,我改变class DocumentViewModelinterface IDocumentViewModel和, ,问题就迎刃而解了。

I'd still like to get this working for inheriting classes, but that's more my stubbornness than any business case. 我仍然希望在继承类方面做到这一点,但这比任何商业案例都更加固执。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM