Overridding ComponentModel attributes (ie, DisplayName) doesn't work as expected
-
26-09-2019 - |
문제
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.
제휴하지 않습니다 StackOverflow