Dude Pascalou
Dude Pascalou

Reputation: 3171

Use DisplayAttribute on class

As we can use System.ComponentModel.DataAnnotations.DisplayAttribute to set a label for a property, I want to use it for the class but it is not allowed on classes.

using System.ComponentModel.DataAnnotations;

[Display(Name = "A person")]
public class Person
{
    [Display(Name = "A name")]
    public string Name { get; set; }
}

Is anyone know a workaround for that ?

EDIT : I want to use it on a strongly typed view. When I create a new strongly typed view, the class name is hard coded in HTML, like that :

@model Models.Person
<fieldset>
    <legend>Person</legend>
    <div class="display-label">
        @Html.LabelFor(model => model.Name)
    </div>
</fieldset>

I want to do something similar to the Name property.

Upvotes: 8

Views: 18504

Answers (3)

Blake
Blake

Reputation: 329

Using the Decorator Pattern, just wrap the DisplayAttribute with your own custom Attribute specifically for classes.

using System;
using System.ComponentModel.DataAnnotations;

namespace YourNameSpaceHere.Support
{
    [AttributeUsage(AttributeTargets.Class)]
    public class DisplayForClassAttribute : Attribute
    {
        protected readonly DisplayAttribute Attribute;

        public DisplayForClassAttribute()
        {
            this.Attribute = new DisplayAttribute();
        }

        public string ShortName
        {
            get { return this.Attribute.ShortName; }
            set { this.Attribute.ShortName = value; }
        }

        public string Name
        {
            get { return this.Attribute.Name; }
            set { this.Attribute.Name = value; }
        }

        public string Description
        {
            get { return this.Attribute.Description; }
            set { this.Attribute.Description = value; }
        }

        public string Prompt
        {
            get { return this.Attribute.Prompt; }
            set { this.Attribute.Prompt = value; }
        }

        public string GroupName
        {
            get { return this.Attribute.GroupName; }
            set { this.Attribute.GroupName = value; }
        }

        public Type ResourceType
        {
            get { return this.Attribute.ResourceType; }
            set { this.Attribute.ResourceType = value; }
        }

        public bool AutoGenerateField
        {
            get { return this.Attribute.AutoGenerateField; }
            set { this.Attribute.AutoGenerateField = value; }
        }

        public bool AutoGenerateFilter
        {
            get { return this.Attribute.AutoGenerateFilter; }
            set { this.Attribute.AutoGenerateFilter = value; }
        }

        public int Order
        {
            get { return this.Attribute.Order; }
            set { this.Attribute.Order = value; }
        }

        public string GetShortName()
        {
            return this.Attribute.GetShortName();
        }

        public string GetName()
        {
            return this.Attribute.GetName();
        }

        public string GetDescription()
        {
            return this.Attribute.GetDescription();
        }

        public string GetPrompt()
        {
            return this.Attribute.GetPrompt();
        }

        public string GetGroupName()
        {
            return this.Attribute.GetGroupName();
        }

        public bool? GetAutoGenerateField()
        {
            return this.Attribute.GetAutoGenerateField();
        }

        public bool? GetAutoGenerateFilter()
        {
            return this.Attribute.GetAutoGenerateFilter();
        }

        public int? GetOrder()
        {
            return this.Attribute.GetOrder();
         }  
     }
 }

Usage would be as follows:

[DisplayForClass(Name = "Approval Matrix")]
public class ApprovalMatrixViewModel
{
}

Upvotes: 1

Jorge
Jorge

Reputation: 18237

I really don't know if it's there another way to do this, but i usually to not hard code this i use create a variable in the view and then i called where i needed. In your case to do it a little more elegant i'll do

@{
    var viewName = typeof(Foo).Name;
}

@model Models.Person
<fieldset>
<legend>@viewName</legend>
<div class="display-label">
    @Html.LabelFor(model => model.Name)
</div>
</fieldset>

Upvotes: 1

PhilPursglove
PhilPursglove

Reputation: 12589

The DisplayName attribute (from System.ComponentModel) performs a similar function and can be applied to a class.

MSDN

Upvotes: 6

Related Questions