DisplayNameFor – DisplayFor – ActionLink & Data Annotation – Session 33

DisplayNameFor DisplayFor ActionLink - Session33

During this session of ASP.NET Core Tutorial (ASP.NET 5.0), we introduce DisplayNameFor, DisplayFor and ActionLink Html Hepler, then we will discuss about Data Annotation. Also, we will create another View with the above mentioned Html Helpers and change properties’ label and data format in the view with Data Annotation.

What is Data Annotation in ASP.NET?

Data Annotation is a tool that we can apply our rules on the models. With this tool we can add validation to our models and forms. Also, we can change property label and Data Format then ASP.NET will take care of enforcing them and displaying appropriate messages to our users.

DisplayFor & DisplayNameFor

These two Html Heplers are responsible for showing labels and values of Model Properties inside the View. DisplayNameFor is showing property’s label and DisplayFor is presenting property’s value inside the view.

Both helpers are bound to the properties of the View Model with Lambda Expression. Also we can change the property’s label and the format of value data with Data Annotation that we will discuss later in this session.

We continue to develop our project that we have been working throughout this tutorial. Thus, we return a View with name of “View” as a result of CreateStronglyTypedView Action Method with Post Method and pass the submitted data to the view.

Then, we create another strongly typed view with name of “View” in Views/Cost folder and bind it to CreateCostViewModel with model directive. Afterward, we can present the submitted data with DisplayNameFor and DisplayFor Html Helper.

This Html Helper, helps us to create <a></a> tag that link us to respective controller and action method. ActionLink has several versions of Overloaded that we can select the proper one based on our needs. But in this session we use the version which includes link label, action method name and controller name.

Bear in mind, if target of the link and the location of the link are in the same controller, we can omit controller name part and use another overloaded version.

You can find the full code of our view in below piece of code.

Data Annotation

This tool is located in “System.ComponentModel.DataAnnotations” namespace and enable us to validate models and forms. Also, we can use Data Annotation to apply our required rules on the model and change properties’ label and data format then ASP.NET will take care of enforcing them and displaying appropriate messages to user.

We can decorate our property with [Display(Name=””)] to change property’s label.

As you can see in above code, we changed Amount, Category and PaymentMethod properties’ label with “Display” Data Annotation. Also, we can change Date Format with [DisplayFormat(DataFormatString =””)] data annotation.

You can find more data type format in THIS LINK, also list of Date Format is available in THIS LINK.

Moreover, we can show proper message to user for null properties. For instance, if we choose “Select a Category” which has value of null as Category, then, in the view we have null value in front of Category field. But if we decorate Category field with [DisplayFormat(NullDisplayText =”Category Not Selected”)] we will have this message if user choose “Select a Category” as Category option.

You find below for full code of CreateCostViewModel.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram or YouTube. Also you can have access to list of all sessions HERE.

You can download this Session Slides form HERE

0.00 avg. rating (0% score) - 0 votes


0 0 votes
Article Rating

Inline Feedbacks
View all comments