【问题标题】:Inheriting view model and changing DisplayName data annotations继承视图模型并更改 DisplayName 数据注释
【发布时间】:2014-04-05 21:47:33
【问题描述】:

我有一个从基类视图模型继承的视图模型。我正在尝试更改继承类中的 DisplayName 元数据,但它不起作用。

这里是视图模型:

namespace ViewModels
{
    public class BaseViewModel
    {
        [DisplayName(Name = "Base Description")]
        public virtual string Description { get; set; }
    }

     public class DerivedViewModel : BaseViewModel
     {
        [DisplayName(Name = "Derived Description")]
        public override string Description { get; set; }
    }
}

还有控制器:

    public ViewResult Create()
    {
        DerivedViewModel model = new DerivedViewModel();
        model.Active = true;
        return View(model);
    }

当视图被渲染时,预期的显示名称是“Derived Description”,但我得到的是“Base Description”。

使用:MVC 5.1、.NET Framework 4.5、Visual Studio 2013

谁能告诉我如何覆盖派生类中的显示数据注释?

【问题讨论】:

  • 你还记得你是否能够解决这个问题吗?

标签: c# inheritance data-annotations asp.net-mvc-5.1


【解决方案1】:

现在已修复。引入了最新的 jQuery 和 jQuery unobtrusive,这解决了这个问题。

【讨论】:

  • 我想知道您是否可以再解释一下这个答案。您在原始问题中面临的问题似乎正是我所面临的。但是,我不明白 JQuery 与显示名称有什么关系?也许这个答案是针对不同的问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
相关资源
最近更新 更多