Search code examples
c#asp.net-mvc-2system.componentmodel

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?


Solution

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