DEV Community

Cover image for Understanding Data Annotations in .NET with a Practical Example
Azizul Arif
Azizul Arif

Posted on

Understanding Data Annotations in .NET with a Practical Example

When building web applications with ASP.NET, data annotations are an essential tool for defining validation rules, display formatting, and model behaviors directly in your code. By using data annotations, you can simplify your code, improve maintainability, and ensure data integrity without writing extensive custom validation logic.

What Are Data Annotations?

Data annotations are attributes you can apply to model properties in .NET to enforce validation rules, control display formatting, and provide metadata about the data model. They help reduce repetitive code and streamline the validation process in your application.

Practical Example: Data Annotations in Action

Let's consider a basic example using a class named Villa. This class represents an entity in a hotel or rental application where users can manage different villas. One of the properties, Price, is decorated with a data annotation to specify how it should be displayed in the UI.

public class Villa
{
    public int Id { get; set; }
    public string Name { get; set; }

    [Display(Name="Price Per Night")]
    public double Price { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Using the [Display] Attribute

In the example above, the Price property uses the [Display] attribute to specify how the property name should be presented in the UI. Instead of the default label "Price," it will be displayed as "Price Per Night," which provides clearer information to the user.

Why Use Data Annotations?

Readability: Data annotations make your code more readable and self-explanatory. By looking at the model, you can quickly understand the intended behavior and validation rules.

Maintainability: Centralizing your validation logic within your models makes it easier to maintain and update as your application evolves.

Consistent UI: Annotations like [Display] ensure that your UI consistently reflects the same labels and formats, reducing discrepancies across your application.

Example in a View

Here's how you might use this model in an ASP.NET Razor view:

<form method="post" class="row">
    <div class="form-floating py-2 col-md-12">
        <input asp-for="Name" class="form-control shadow border" />
        <label asp-for="Name" class="ms-2 text-black"></label>
    </div>
    <div class="form-floating py-2 col-md-12">
        <input type="number" asp-for="Price" class="form-control shadow border" />
        <label asp-for="Price" class="ms-2 text-black"></label>
    </div>

</form>
Enter fullscreen mode Exit fullscreen mode

In this form, the Price field will display "Price Per Night" as its label, thanks to the [Display] attribute applied to the model property.

Conclusion
Data annotations are a powerful feature in .NET that allow developers to enforce validation rules, control display formatting, and provide metadata directly within their models. By leveraging these attributes, you can write cleaner, more maintainable, and user-friendly applications.

Top comments (0)