jimmy
jimmy

Reputation: 756

.net core data annotation display Name - inherite to viewmodels

I am trying to create a ASP.NET application, and am using DataAnnotations in the Entity Class Models for more readable display names:

In my ApplicationDomain Project

public class Car{
    public int Id { get; set; }
    [Display(Name = "Make of Car")]
    public string Make { get; set; }

    [Display(Name = "Year of Purchase")]
    public int PurchaseYear { get; set; }

}

When I then use this as the model for my views, everything is displayed as expected.

But when I use a view model, I then have to add the Annotations again as the Display Name I initially added to Car is not 'Inherited' to the View Models based on it.

In my WebMVC Project

public class EditCarViewModel{

    [Display(Name = "Make of Car")]
    public string Make { get; set; }

    [Display(Name = "Year of Purchase")]
    public int PurchaseYear { get; set; }
}

The same for the create, index and any other views that use a viewmodel and not the Car Class.

Is there anyway to have the annotations that are in the initial entity class model inherited / propagated up, into the related view models so I'm not having to do this in multiple places?

I think this will be even more of an issue if I then try to add a different UI project. e.g. a desktop application in addition to the WebMVC.

It would be ideal if the labels for both could be based on the definitions in the ApplicationDomain Project.

Upvotes: 0

Views: 1040

Answers (2)

Dmitry Pavlov
Dmitry Pavlov

Reputation: 28290

There is no way of propagating annotation text from one class to another.

But if you just want to keep the same text in one place, you can create constants and use them this way:

public static class DisplayConstants
{
    public const string Make = "Make of Car";
    public const string PurchaseYear = "Year of Purchase";
}

public class EditCarViewModel{

    [Display(Name = DisplayConstants.Make)]
    public string Make { get; set; }

    [Display(Name = DisplayConstants.PurchaseYear)]
    public int PurchaseYear { get; set; }
}

public class Car
{
    public int Id { get; set; }

    [Display(Name = DisplayConstants.Make)]
    public string Make { get; set; }

    [Display(Name = DisplayConstants.PurchaseYear)]
    public int PurchaseYear { get; set; }
}

Note that this way you can name properties in EditCarViewModel and Car whatever way you like, no restriction on consistent naming.

Upvotes: 0

JamieD77
JamieD77

Reputation: 13949

You can try creating a new metadata class and apply it to your others.

[MetadataType(typeof(CarModelMetaData))]
public class EditCarViewModel{
    public string Make { get; set; }.
    public int PurchaseYear { get; set; }
}

[MetadataType(typeof(CarModelMetaData))]
public class CreateCarViewModel{
    public string Make { get; set; }
    public int PurchaseYear { get; set; }
}

public class CarModelMetaData{

    [Display(Name = "Make of Car")]
    public string Make { get; set; }

    [Display(Name = "Year of Purchase")]
    public int PurchaseYear { get; set; }
}

Upvotes: 1

Related Questions