CRUD Operations In ASP.NET Core 1.0 MVC Application - Part Two

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.
  • Open existing Solution in Visual Studio 2015.
  • Add new folder, Models, in the root of WebApplicationCore.NetCore Project.
  • Now, create a sub-folder, Contact, in Models folder.
  • Now, add a new class ContactVM.CS in Models >> Contact.
  • Add required properties in ContactVM.
    1. public class ContactVM    
    2.   {    
    3.     public Contact()    
    4.     {    
    5.     }    
    6.     public int ContactId { get; set; }    
    7.     public string Name { get; set; }    
    8.     public string Address1 { get; set; }    
    9.     public string Address2 { get; set; }    
    10.     public string City { get; set; }    
    11.     public string ProvinceState { get; set; }    
    12.     public string ZipPostalCode { get; set; }    
    13.     public string Country { get; set; }    
    14.     public string ContactNumber { get; set; }    
    15.     public string Email { get; set; }    
    16.     public string WebSite { get; set; }    
    17.   }  
    code

Update Contact GetContact View

  • Change _ViewImports.cshtml by including the namespace of WebApplicationCore.NetCore.Models. So, we can access Contact View Model without fully qualified name.

  • Use ContactVM as model in the View, instead of Contact.

  • Update GetContact View - Use LabelTagHelper to display the field headings from View Model instead of hard coded text.

    • Here, we have used LabelTagHelper which uses the text from provided data filed. If provided has Display Attribute, then it will use text from this attribute. Alternatively, it will display the field name. We just have to provide asp-for tag (or other asp- depending on Tag Helper) with field name from Model.

    • On the other hand, we have to just display the text. So, there is ideally no need to use any data notation for a simple View.

    • Tag Helpers are special features used to enable server side code to create, update, and render the HTML in View file. They are said to be evaluations of HTML Helpers. Although, HTML Helpers do exist seperately, Tag Helpers are very popular as they are just additional attributes in existing HTML elements and are very easy for UI designers or front stack developers to understand. We will discuss Tag Helpers in detail in future sessions.
      1. @model ContactVM    
      2. <h2>Contact</h2>    
      3. <div>    
      4.   <dl class="dl-horizontal">    
      5.     <dt>    
      6.       <label asp-for="Name"></label>    
      7.     </dt>    
      8.     <dd>    
      9.       <span>@Model.Name</span>    
      10.     </dd>    
      11.     <dt>    
      12.       <label asp-for="Address1"></label>    
      13.     </dt>    
      14.     <dd>    
      15.       <span>@Model.Address1</span>    
      16.     </dd>    
      17.     <dt>    
      18.       <label asp-for="Address2"></label>    
      19.     </dt>    
      20.     <dd>    
      21.       <span>@Model.Address2</span>    
      22.     </dd>    
      23.     <dt>    
      24.       <label asp-for="City"></label>    
      25.     </dt>    
      26.     <dd>    
      27.       <span>@Model.City</span>    
      28.     </dd>    
      29.     <dt>    
      30.       <label asp-for="ProvinceState"></label>    
      31.     </dt>    
      32.     <dd>    
      33.       <span>@Model.ProvinceState</span>    
      34.     </dd>    
      35.     <dt>    
      36.       <label asp-for="ZipPostalCode"></label>    
      37.     </dt>    
      38.     <dd>    
      39.       <span>@Model.ZipPostalCode</span>    
      40.     </dd>    
      41.     <dt>    
      42.       <label asp-for="Country"></label>    
      43.     </dt>    
      44.     <dd>    
      45.       <span>@Model.Country</span>    
      46.     </dd>    
      47.     <dt>    
      48.       <label asp-for="ContactNumber"></label>    
      49.     </dt>    
      50.     <dd>    
      51.       <span>@Model.ContactNumber</span>    
      52.     </dd>    
      53.     <dt>    
      54.       <label asp-for="Email"></label>    
      55.     </dt>    
      56.     <dd>    
      57.       @if (!string.IsNullOrWhiteSpace(Model.Email))    
      58.       {    
      59.         <a href="mailto:@Model.Email">@Model.Email</a>    
      60.       }    
      61.     </dd>    
      62.     <dt>    
      63.       <label asp-for="WebSite"></label>    
      64.     </dt>    
      65.     <dd>    
      66.       @if (!string.IsNullOrWhiteSpace(Model.WebSite))    
      67.       {    
      68.         <a href="@Model.WebSite" target="_blank">@Model.WebSite</a>    
      69.       }    
      70.     </dd>    
      71.   </dl>    
      72. </div>     
      code

Update GetContact Action Method

  • Change GetContact Action Method in ContactController.

    • After getting the data object from ContactBusinessLogic, transform it into ContactVM object.

    • Pass contactVM object to View.
      1. using System;    
      2. using System.Collections.Generic;    
      3. using System.Linq;    
      4. using System.Threading.Tasks;    
      5. using Microsoft.AspNetCore.Mvc;    
      6. using WebApplicationCore.NetCore.BusinessLogic;    
      7. using WebApplicationCore.NetCore.Model;    
      8. using WebApplicationCore.NetCore.Models;    
      9. // For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860    
      10. namespace WebApplicationCore.NetCore.Controllers    
      11. {    
      12.   public class ContactController : Controller    
      13.   {    
      14.     // GET: /<controller>/    
      15.     public IActionResult Index()    
      16.     {    
      17.       return View();    
      18.     }    
      19.     public IActionResult GetContact(int id)    
      20.     {    
      21.       ContactBusinessLogic contactBL = new ContactBusinessLogic();    
      22.       Contact contact = contactBL.GetContact(id);    
      23.       ContactVM contactVM = new ContactVM    
      24.       {    
      25.         Address1 = contact.Address1,    
      26.         Address2 = contact.Address2,    
      27.         City = contact.City,    
      28.         ContactId = contact.ContactId,    
      29.         ContactNumber = contact.ContactNumber,    
      30.         Country = contact.Country,    
      31.         Email = contact.Email,    
      32.         Name = contact.Name,    
      33.         ProvinceState = contact.ProvinceState,    
      34.         WebSite = contact.WebSite,    
      35.         ZipPostalCode = contact.ZipPostalCode    
      36.       };    
      37.       return View(contactVM);    
      38.     }    
      39.   }    
      40. }   
      code

Run Application in Debug Mode

  • Press F5 or Debug Menu >> Start Debugging or Start IIS Express Button on Toolbar to start application in debugging mode.

  • It will show the Home Page in browser.

  • Type http://localhost:21840/Contact/GetContact/1 in address bar of browser and it will show the Contact details of contact with Id 1.

  • Type http://localhost:21840/Contact/GetContact/2 in address bar of browser and it will show the Contact details of contact with Id 2.

  • Type http://localhost:21840/Contact/GetContact/3 in address bar of browser and it will show the Contact details of contact with Id 3.

  • Now, we can notice that email and website fields are showing as URLs. While there is no change in label names, they are still filed name.

    Run Application

Use Data Annotation

  • Now, add Display Attribute (from System.ComponentModel.DataAnnotations) with suitable heading to every field of ContactVM.

  • Run the application and type http://localhost:21840/Contact/GetContact/1 in address bar of browser. It will show the Contact details of contact with Id 1.

  • Now, we can notice that the View is showing the headings from ContactVM Display Attribute instead of field names.
    1. public class ContactVM    
    2.   {    
    3.     [Display(Name = "Contact Id")]    
    4.     public int ContactId { get; set; }    
    5.     [Display(Name = "Contact Name")]    
    6.     public string Name { get; set; }    
    7.     [Display(Name = "Address 1")]    
    8.     public string Address1 { get; set; }    
    9.     [Display(Name = "Address 2")]    
    10.     public string Address2 { get; set; }    
    11.     [Display(Name = "City")]    
    12.     public string City { get; set; }    
    13.     [Display(Name = "Province/State")]    
    14.     public string ProvinceState { get; set; }    
    15.     [Display(Name = "Zip/Postal Code")]    
    16.     public string ZipPostalCode { get; set; }    
    17.     [Display(Name = "Country")]    
    18.     public string Country { get; set; }    
    19.     [Display(Name = "Contact Number")]    
    20.     public string ContactNumber { get; set; }    
    21.     [Display(Name = "Email")]    
    22.     public string Email { get; set; }    
    23.     [Display(Name = "Web Site")]    
    24.     public string WebSite { get; set; }    
    25.   }   
    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.

Up Next
    Ebook Download
    View all
    Learn
    View all