Inheriting view model and changing DisplayName data annotations
Asked Answered
S

1

7

I have a view model which I am inheriting from a base class view model. I am trying to change the DisplayName metadata in the inherited class but it's not working.

Here are the view models:

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; }
    }
}

And the controller:

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

When the view is rendered, the expected display name is "Derived Description" but instead I'm getting "Base Description".

Using: MVC 5.1, .NET Framework 4.5, Visual Studio 2013

Can anyone tell me how to override the Display data annotation in a derived class?

Settles answered 4/3, 2014 at 11:23 Comment(1)
Do you remember if you were able to resolve this?Helix
S
0

This is now fixed. Brought in the latest jQuery and jQuery unobtrusive and this fixed the issue.

Settles answered 5/3, 2014 at 9:3 Comment(1)
I'm wondering if you could explain this answer a little more. The problem you were facing in the original question seems exactly what I'm facing. However, I don't understand how JQuery has anything to do with display name? Perhaps this answer was intended for a different question?Helix

© 2022 - 2024 — McMap. All rights reserved.