DisplayTextFor, DisplayForModel, DataType & ScaffoldColumn – Session 34

DisplayTextFor DisplayForModel DataType ScaffoldColumn - Session34

In this session of ASP.NET Core Tutorial, we discuss about DisplayTextFor and DisplayForModel Html Helpres, then we will talk about DataType and ScaffoldColumn attributes. Also, we will show you how to find a record in database or even static list in ASP.NET Core and with this technique we retrieve Method name of PaymentMethod property to be displayed.

DisplayTextFor & DisplayForModel

DisplayTextFor is working exactly same as DisplayFor which we discussed about that in Session 33. Thus, we can replace DisplayFor with DisplayTextFor and bind it to property of Model with Lambda Expression.

Also, we can show all the properties of a model with a single line of code when we use DisplayForModel Html Helper. Thus, this line of code, can be replaced with all DisplayNameFor and DisplayFor or DisplayTextFor in the View.

Searching for a Record in Database in ASP.NET Core

In previous session, we displayed submitted data from CreateStronglyTypedView in View which we called it View inside Views\Cost and displayed ID of PaymentMethod instead of Its Method name. In this session, we develop our Web App to show Method name instead of its ID. For this purpose, we should search for a record in our Payment Method list, then return the Method name. Hence, first of all we need a new ViewModel that its PaymentMethod property be in String data type.

Then, declare a method in IPaymentMethodRepository to be used for retreating Method name instead of Method ID (GetPaymentMethodbyID).

Now, we need to implement this method in StaticPaymentMethodRepository. Within this method, we search for a specific record in PaymentMethod list which its ID is equal selected ID that passed to this Method with input parameter. Then, we return Method property of the record.

Also, we need to create an instance of CostRegisteredViewModel inside Post part of CreateStronglyTypedView Action Method in CostController. Moreover we should fill the properties of that object with submitted data. Just bear in mind, PaymentMethod property type of submitted data is Int and we should retrieve Method name with help of GetPaymentMethodbyID Method.

Moreover, we should change the model directive of View to be bound to CostRegisteredViewModel.

Hiding a Property in View with ScaffoldColumn

As earlier mentioned, when we use DisplayForModel, all the properties of the Model are displayed in view. In case that we want to hide one or some of the properties in View. We can use ScaffoldColumn(false) for those properties.

For example in above code, we hide PaymentMethod property in the View as it ScaffoldColumn attribute is set to false.

Attention: this attribute just work with DisplayForModel Html Helper and if we use DisplayFor or DisplayTextFor Html Helper, value and label of the property will be displayed in the VIew.

DataType Attribute

We can modify Display Format of the properties of the Model with DataType attribute. With this Data Annotation, we can display the properties with proper format and attribute. There is several type of DataType same as Password, Currency, EmailAddress, Url, Text and etc. that can be used based on our needs. For instance, if we use Url DataType for a relative or absolute URL, it will be displayed in View as a hyperlink that can be clicked.

In this session we test some of them on CostRegisteredViewModel to show you how you can use them on your Models.

Result of using DataType on View

Changing Currency Symbol in ASP.NET Core

For changing currency symbol in ASP.NET Core, we should use Globalization and CultureInfo in Configure Method of Startup Class. Moreover, we can change country and language with this feature as well. Thus, for changing currency symbol in our Web App, first we need to add System.Globalization namespace to StartupClass.

Then, we should create an instance of CultureInfo method and set language and country with their codes. Next, we can set our required Currency Symbol and make our web app DefaultThreadCurrentCulture and DefaultThreadCurrentUICulture equal to the changes.

Above code, set country as United Kingdom and language as English. Also, as you can see Currency Symbol is changed to “£”. You can see LIST of Country and Language Code HERE.

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