Let’s first add View Model in application flow. Then, we will discuss data annotation and Tag Helpers. We have created a "Get Details" functionality in our previous discussion.

We can see that we have hard coded headings and embedded the code to generate required HTML. Although this approach works fine for simple Views, we have to take care of a lot of things and write a lot of code to handle the features like forms and validations.

Similarly, it is not a good practice to use Models associated with Data Source, like Entity Framework or Data Contracts from Services in Views. Instead of them, very concise and simple Models are passed to Views, which is called ViewModels. These ViewModels are based on the concept of POCO (Plain Old CLR Object). We will discuss pros and cons of POCO or ViewModels in detail, in a different session.

Add Contact View Model in Project.

Update Contact GetContact View

Update GetContact Action Method

Run Application in Debug Mode

Use Data Annotation

Sample Source Code

I have placed sample code for this session in "CRUD operations in ASP.NET Core 1.0 MVC Application Part 2_Code.zip" in the CodePlex repository.