Bedouin
Bedouin

Reputation: 500

Set DisplayPropertyName value

I would like to know if there is a maneer (may be with data annotation) to set the value of

property.DisplayPropertyName

.

Do you have any idea please?!!

Upvotes: 0

Views: 356

Answers (2)

Pierre America
Pierre America

Reputation: 146

Actually I think you mean to ask how you can choose a different property as the display property (i.e., the property that is used to briefly denote objects of your class in the UI).

The Entity Framework Service that generates the metadata for scaffolding choose the first property of string type (as far as I know), but you can change that by applying the DisplayColumn attribute to your class:

[DisplayColumn("Alias")]
public class Person {
    public string Name { get; set; }
    public string Alias { get; set; }
}

Note, however, that this only works for properties that are actually mapped to a column in the database. (I was trying to use a computed property, not mapped, but that led to an error message while scaffolding.)

Upvotes: 2

Rvcl
Rvcl

Reputation: 64

That is quite easy to do:

[DisplayName("RenamedProperty")]

you can even add a description

[Description("Demonstrates DisplayNameAttribute.")]

or perhaps you would like it to reference your resources so it adapts itself to the selected language:

[Display(Name = "Description", ResourceType = typeof(Resources))]

Upvotes: 1

Related Questions