Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 398
Dude Pascalou Avatar asked Jul 02 '12 21:07

Dude Pascalou


People also ask

How do you display attributes in C#?

For that we can use the Display attribute. DisplayAttribute or Display or DisplayName attribute: DisplayAttribute and Display is present in the System. ComponentModel. DataAnnotations namespace.

What is DisplayName attribute?

DisplayName. Gets the display name for a property, event, or public void method that takes no arguments stored in this attribute.


1 Answers

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

MSDN

like image 161
PhilPursglove Avatar answered Oct 22 '22 09:10

PhilPursglove