DisplayTemplates & EditorTemplates – Session 36

DisplayTemplates EditorTemplates in ASP.NET Core - Session36

In this session of ASP.NET Core Tutorial we talk about DisplayTemplates and EditorTemplates and customize the Views that we created in session 34 and 35 with DisplayForModel and EditorForModel. As their names imply, DisplayTemplates is for customizing Views that are created by DisplayForModel. Moreover, EditorTemplates is able to customize Views which are implemented by EditorForModel.

DisplayTemplates in ASP.NET Core MVC

To customize a View that was implemented by DisplayForModel, first we should create a folder with name of DisplayTemplates in Views/Shared folder. But, if we want to modify just views of a specific controller, we can create above mentioned folder in Views/ControllerName. Then, for modifying each property or group of properties we should create a View file inside above mentioned created folder with specific name.

For this purpose, we should create a View file in Views/Shared/DisplayTemplates folder with name of Url.cshtm. Then, use Anchor Tag inside the file and use ViewData.Model as href and label of the link.

location of DisplayTemplates and EditorTemplates folders

Customize Properties with String type with DisplayTemplates in ASP.NET Core MVC

For this purpose we should create a View file in Views/Shared/DisplayTemplates folder with name of string.cshtm. Then, inside this file, we can modify style of the properties with type of “String”. Also, for accessing the value of the property, we can use ViewData.Model.

Change DateTime display format with DisplayTemplates in ASP.NET Core MVC

For this purpose we should create a View file in Views/Shared/DisplayTemplates folder with name of DateTime.cshtm. Then, bind the View to DateTime type with Model Directive and for accessing the value of the property we can use @Model. Moreover, for customizing the DateTime format we can convert the value to string with ToString() method. Moreover, we can set the format as input parameter of ToString(“dd/MM/yyyy”) method.

EditorTemplate in ASP.NET Core MVC

We can customize the Edit Form that use EditorForModel with EditorTemplates, for this purpose we should create a folder with name of EditorTemplates in Views/Shared folder. But, if we want to modify just views of a specific controller, we can create above mentioned folder in Views/ControllerName. Then, for modifying each property or group of properties, we should create a View file inside above mentioned created folder with specific name.

Change DateTime format in Textbox with EditorTemplate

For changing DateTime format in Edit Form, we can create a View file inside Views/Shared/EditorTemplates folder with name of DateTime.cshtml. Then, bind the view to DateTime with Model Directive. Next, use TextBox Html Helper and use Model.ToString(“dd/MMM/yyyy”) for customizing the format.

Customize TextBox of properties with String type

For this purpose, this time we create a view file inside Views/Shared/EditorTemplates with name of string.cshtml. Then, with help of TextBox HtmlHelper and Html Attribute we can modify the TextBox of properties with String type.

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

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments