覆盖 ComponentModel 属性(即 DisplayName)无法按预期工作
我有两个类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经想出了解决我自己问题的方法。它并不完美,但还可以。
由于这些只是 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
tointerface 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.