DEV Community

Sardar Mudassar Ali Khan
Sardar Mudassar Ali Khan

Posted on

Using displayname, displayformat, scaffoldcolumn attributes in asp net mvc application

In an ASP.NET MVC application, you can use various attributes like DisplayName, DisplayFormat, and ScaffoldColumn to customize the way properties are displayed in views. These attributes provide metadata to the view engine and allow you to control the rendering of data.

  1. DisplayName Attribute: The DisplayName attribute allows you to specify a friendly name or label for a property that will be displayed in views. It is mainly used to provide a more user-friendly name for properties instead of using the property name itself. Here's an example:
public class MyModel
{
    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [DisplayName("Last Name")]
    public string LastName { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the DisplayName attribute is used to specify custom names for the FirstName and LastName properties.

  1. DisplayFormat Attribute: The DisplayFormat attribute allows you to control the formatting of a property's value in views. It is useful when you want to display dates, numbers, or other data types in a specific format. Here's an example:
public class MyModel
{
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime BirthDate { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the DisplayFormat attribute is used to specify that the BirthDate property should be displayed in the format "yyyy-MM-dd".

  1. ScaffoldColumn Attribute: The ScaffoldColumn attribute controls whether a property should be automatically rendered in views. By default, properties are scaffolded (rendered) in views, but you can use this attribute to exclude certain properties. Here's an example:
public class MyModel
{
    public string Property1 { get; set; }

    [ScaffoldColumn(false)]
    public string Property2 { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Property1 will be automatically scaffolded in views, but the Property2 will be excluded from scaffolding.

These attributes can be used in conjunction with other ASP.NET MVC features like HTML helpers and data annotations to provide a rich and customizable user interface in your application's views.

Top comments (0)