简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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