In C#, both DisplayName
attribute and Display
attribute are used to provide a user-friendly name or label for a property or a field. However, they have slightly different usage scenarios.
The DisplayName
attribute is used with the System.ComponentModel
namespace and is typically used to set the display name of a property or a field. This attribute is used primarily for data binding scenarios and is used to provide a human-readable name for a property that is displayed in a UI, such as a form or a data grid.
For example, the following code demonstrates how to use the DisplayName
attribute to provide a user-friendly name for a property:
public class Customer { [DisplayName("Customer ID")] public int CustomerID { get; set; } // other properties }
The Display
attribute, on the other hand, is used with the System.ComponentModel.DataAnnotations
namespace and provides additional metadata for a property or a field, including the display name, description, order, and format. This attribute is often used in conjunction with the EditorFor
and DisplayFor
HTML helpers in ASP.NET MVC applications to automatically generate UI elements based on the metadata.
For example, the following code demonstrates how to use the Display
attribute to provide additional metadata for a property:
public class Product { [Display(Name = "Product ID", Order = 1)] public int ProductID { get; set; } [Display(Name = "Product Name", Order = 2)] public string ProductName { get; set; } [Display(Name = "Unit Price", Order = 3, Format = "{0:c}")] public decimal UnitPrice { get; set; } // other properties }
In summary, the DisplayName
attribute is primarily used to set the display name of a property for data binding scenarios, while the Display
attribute provides additional metadata for a property, including the display name, description, order, and format, and is often used in ASP.NET MVC applications.
"C# DisplayName vs Display Attribute Basics"
DisplayName
and Display
attributes.public class MyModel { [DisplayName("First Name")] [Display(Name = "Last Name")] public string FirstName { get; set; } }
DisplayName
and Display
attributes for properties in a model."C# DisplayName vs Display Attribute with Description"
Display
attribute to include a description for a property.public class MyModel { [DisplayName("First Name")] [Display(Name = "Last Name", Description = "The last name of the person")] public string FirstName { get; set; } }
Description
property of the Display
attribute."C# DisplayName vs Display Attribute with GroupName"
Display
attribute's GroupName
property.public class MyModel { [DisplayName("First Name")] [Display(Name = "Last Name", GroupName = "Personal Info")] public string FirstName { get; set; } }
GroupName
property of the Display
attribute."C# DisplayName vs Display Attribute with Prompt"
Display
attribute's Prompt
property for providing a prompt message.public class MyModel { [DisplayName("First Name")] [Display(Name = "Last Name", Prompt = "Enter last name")] public string FirstName { get; set; } }
Prompt
property of the Display
attribute to provide a prompt message."C# DisplayName vs Display Attribute with ResourceType"
Display
attribute.public class MyModel { [DisplayName("First Name")] [Display(Name = "Last Name", ResourceType = typeof(Resources))] public string FirstName { get; set; } }
ResourceType
property of the Display
attribute for localization with resource files."C# DisplayName vs Display Attribute with AutoGenerateField"
Display
attribute.public class MyModel { [DisplayName("First Name")] [Display(Name = "Last Name", AutoGenerateField = false)] public string FirstName { get; set; } }
AutoGenerateField
property of the Display
attribute."C# DisplayName vs Display Attribute with Order"
Display
attribute.public class MyModel { [DisplayName("First Name")] [Display(Name = "Last Name", Order = 2)] public string FirstName { get; set; } }
Order
property of the Display
attribute."C# DisplayName vs Display Attribute with ShortName"
Display
attribute.public class MyModel { [DisplayName("First Name")] [Display(ShortName = "LN")] public string FirstName { get; set; } }
ShortName
property of the Display
attribute."C# DisplayName vs Display Attribute with Watermark"
Display
attribute.public class MyModel { [DisplayName("First Name")] [Display(Prompt = "Enter last name", Watermark = "Last Name")] public string FirstName { get; set; } }
Watermark
property of the Display
attribute to provide a watermark for the property."C# DisplayName vs Display Attribute with NullDisplayText"
Display
attribute's NullDisplayText
property.public class MyModel { [DisplayName("First Name")] [Display(NullDisplayText = "No last name provided")] public string FirstName { get; set; } }
NullDisplayText
property of the Display
attribute.polynomial-math time.h android-4.4-kitkat dao wildfly-10 underline spark-avro ngx-datatable type-mismatch control-structure