Adrian Thompson Phillips
Adrian Thompson Phillips

Reputation: 7141

How to use MetadataTypeAttribute with extended classes

I want to add a DisplayAttribute to the Client entity (from another project), but don't want to pollute my entity with attributes specific to MVC or a UI layer. So I planned to add the DisplayAttribute by applying a metadata class to a view model inheriting from the entity

If I inherit from the Client entity and then try to use the MetadataTypeAttribute to add a display attribute, it doesn't show up in the browser. Does anyone know how I can achieve the separation and the functionality of being able to add metadata to my entities?

The Client entity class:

public class Client
{
    private string firstName;

    private string lastName;

    private string homeTelephone;

    private string workTelephone;

    private string mobileTelephone;

    private string emailAddress;

    private string notes;

    public Title Title { get; set; }

    public string FirstName
    {
        get { return this.firstName ?? string.Empty; }

        set { this.firstName = value; }
    }

    public string LastName
    {
        get { return this.lastName ?? string.Empty; }

        set { this.lastName = value; }
    }

    public string FullName
    {
        get
        {
            List<string> nameParts = new List<string>();

            if (this.Title != Title.None)
            {
                nameParts.Add(this.Title.ToString());
            }

            if (this.FirstName.Length > 0)
            {
                nameParts.Add(this.FirstName.ToString());
            }

            if (this.LastName.Length > 0)
            {
                nameParts.Add(this.LastName.ToString());
            }

            return string.Join(" ", nameParts);
        }
    }

    public Address Address { get; set; }

    public string HomeTelephone
    {
        get { return this.homeTelephone ?? string.Empty; }

        set { this.homeTelephone = value; }
    }

    public string WorkTelephone
    {
        get { return this.workTelephone ?? string.Empty; }

        set { this.workTelephone = value; }
    }

    public string MobileTelephone
    {
        get { return this.mobileTelephone ?? string.Empty; }

        set { this.mobileTelephone = value; }
    }

    public string EmailAddress
    {
        get { return this.emailAddress ?? string.Empty; }

        set { this.emailAddress = value; }
    }

    public string Notes
    {
        get { return this.notes ?? string.Empty; }

        set { this.notes = value; }
    }

    public Client()
    {
        this.Address = new Address();
    }
}

The ClientViewModel view model class:

[MetadataType(typeof(ClientMetaData))]
public class ClientViewModel : Client
{
    internal class ClientMetaData
    {
        [Display(ResourceType = typeof(ResourceStrings), Name = "Client_FirstName_Label")]
        public string FirstName { get; set; }
    }
}

Upvotes: 3

Views: 3536

Answers (2)

Marek Pio
Marek Pio

Reputation: 127

For .Net Core 6.0 use

[ModelMetadataType(typeof(ClientViewModel.ClientMetaData))] 

insead of

[MetadataType(typeof(ClientViewModel.ClientMetaData))]

Upvotes: 0

pantuofermo
pantuofermo

Reputation: 170

I think you have change the typeof parameter to:

[MetadataType(typeof(ClientViewModel.ClientMetaData))]
public class ClientViewModel : Client
{
    internal class ClientMetaData
    {
        [Display(ResourceType = typeof(ResourceStrings), Name = "Client_FirstName_Label")]
        public string FirstName { get; set; }
    }
}

Upvotes: 2

Related Questions