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

 We are going to extend our application from the last discussion,  CRUD Operations In ASP.NET Core 1.0 MVC Application - Part Four. And we will be using same existing ContactVM. Let’s add UpdateContact page for our Contacts.

Add UpdateContact method in ContactBusinessLogic

  • Open existing Solution in Visual Studio 2015.
  • Open WebApplicationCore.NetCore.BusinessLogic.ContactBusinessLogic class.
  • Add new UpdateContact method.
  • It may update the existing contact in mockup list data.
    1.  public bool UpdateContact(Contact contact)  
    2. {  
    3.     bool updated = false;  
    4.     Contact existingContact = this.GetContact(contact.ContactId);  
    5.   
    6.     if (existingContact != null)  
    7.     {  
    8.         existingContact.Address1 = contact.Address1;  
    9.         existingContact.Address2 = contact.Address2;  
    10.         existingContact.City = contact.City;  
    11.         existingContact.ContactNumber = contact.ContactNumber;  
    12.         existingContact.Country = contact.Country;  
    13.         existingContact.Email = contact.Email;  
    14.         existingContact.Name = contact.Name;  
    15.         existingContact.WebSite = contact.WebSite;  
    16.         existingContact.ZipPostalCode = contact.ZipPostalCode;  
    17.         updated = true;  
    18.     }  
    19.   
    20.     return updated;  
    21. }    
    code

Add UpdateContact Action Methods in ContactController

  • Add two new UpdateContact methods.

    • One is a CreateContact method with ContactId parameter and HttpGet attribute to explicitly specify it as get method. It will be used to initialize and create the Update Page. We will pass View a ContactVM, having contact data.

      • Get Contact Data from Business Logic for given id.
      • Transform it into ContactVM and pass to view.
      • Logically, this is almost similar to GetContact.

    • The other is an UpdateContact method with parameter of ContactVM type and HttpPost attribute to explicitly specify that this is post method. It will be used to validate input at server side and to update object.

      • Post method may use ModelState.IsValid property to check if model does not have any error. 
      • Transform ContactVM object to Contact object and pass it to ContactBusinessLogic().UpdateContact for further processing.
      • If no error is found there and we have performed all activities successfully, then we can move to the other page. For example, in our case, we may navigate to main list page. Alternatively, we may return the same View with current object for reprocessing.
      • Logically, this is almost similar to CreateContact.

    • It is not required for both methods to have the same name, but it is conventional and "in practice" for a long time.

  • RedirectToAction is used to redirect to some other View.
    1. [HttpGet]  
    2. public IActionResult UpdateContact(int id)  
    3. {  
    4.     ContactBusinessLogic contactBL = new ContactBusinessLogic();  
    5.     Contact contact = contactBL.GetContact(id);  
    6.   
    7.     ContactVM contactVM = new ContactVM  
    8.     {  
    9.         Address1 = contact.Address1,  
    10.         Address2 = contact.Address2,  
    11.         City = contact.City,  
    12.         ContactId = contact.ContactId,  
    13.         ContactNumber = contact.ContactNumber,  
    14.         Country = contact.Country,  
    15.         Email = contact.Email,  
    16.         Name = contact.Name,  
    17.         ProvinceState = contact.ProvinceState,  
    18.         WebSite = contact.WebSite,  
    19.         ZipPostalCode = contact.ZipPostalCode  
    20.     };  
    21.   
    22.     return View(contactVM);  
    23. }  
    24.   
    25. [HttpPost]  
    26. public IActionResult UpdateContact(ContactVM contactVM)  
    27. {  
    28.     if (this.ModelState.IsValid)  
    29.     {  
    30.         Contact contact = new Contact  
    31.         {  
    32.             Address1 = contactVM.Address1,  
    33.             Address2 = contactVM.Address2,  
    34.             City = contactVM.City,  
    35.             ContactId = contactVM.ContactId,  
    36.             ContactNumber = contactVM.ContactNumber,  
    37.             Country = contactVM.Country,  
    38.             Email = contactVM.Email,  
    39.             Name = contactVM.Name,  
    40.             ProvinceState = contactVM.ProvinceState,  
    41.             WebSite = contactVM.WebSite,  
    42.             ZipPostalCode = contactVM.ZipPostalCode  
    43.         };  
    44.         ContactBusinessLogic contactBL = new ContactBusinessLogic();                  
    45.         if (contactBL.UpdateContact(contact))  
    46.         {  
    47.             return RedirectToAction("Index");  
    48.         }  
    49.     }  
    50.     return View(contactVM);  
    51. }  
    code

Add Contact UpdateContact View

  • Add new View to Contact\Contact folder.
  • Open Add New Item screen through Solution. Go to Context Menu of Contact >> Add >> New Item >> Installed >> .NET Core >> MVC View Page.
  • Name it as UpdateContact.cshtml.
  • Click OK.
  • It will add a new View in Contact View folder.

    • Now, we have ContactVM objects as model.
    • Form does not process disabled items, so we have added a hidden field for ContactId, to make it available in Post method.

  • Change Index View implementation to add Update Contact option with each record.
    1. @model ContactVM  
    2. <h2>Update Contact</h2>  
    3. <form asp-action="UpdateContact">  
    4.     <input type="hidden" asp-for="ContactId" />  
    5.     <div class="form-horizontal">  
    6.         <div asp-validation-summary="ModelOnly" class="text-danger"></div>  
    7.         <div class="form-group">  
    8.             <label asp-for="ContactId" class="col-md-2 control-label"></label>  
    9.             <div class="col-md-10">  
    10.                 <input asp-for="ContactId" class="form-control" disabled="disabled" />  
    11.                 <span asp-validation-for="ContactId" class="text-danger" />  
    12.             </div>  
    13.         </div>  
    14.         <div class="form-group">  
    15.             <label asp-for="Name" class="col-md-2 control-label"></label>  
    16.             <div class="col-md-10">  
    17.                 <input asp-for="Name" class="form-control" />  
    18.                 <span asp-validation-for="Name" class="text-danger" />  
    19.             </div>  
    20.         </div>  
    21.         <div class="form-group">  
    22.             <label asp-for="Address1" class="col-md-2 control-label"></label>  
    23.             <div class="col-md-10">  
    24.                 <input asp-for="Address1" class="form-control" />  
    25.                 <span asp-validation-for="Address1" class="text-danger" />  
    26.             </div>  
    27.         </div>  
    28.         <div class="form-group">  
    29.             <label asp-for="Address2" class="col-md-2 control-label"></label>  
    30.             <div class="col-md-10">  
    31.                 <input asp-for="Address2" class="form-control" />  
    32.                 <span asp-validation-for="Address2" class="text-danger" />  
    33.             </div>  
    34.         </div>  
    35.         <div class="form-group">  
    36.             <label asp-for="City" class="col-md-2 control-label"></label>  
    37.             <div class="col-md-10">  
    38.                 <input asp-for="City" class="form-control" />  
    39.                 <span asp-validation-for="City" class="text-danger" />  
    40.             </div>  
    41.         </div>  
    42.         <div class="form-group">  
    43.             <label asp-for="ProvinceState" class="col-md-2 control-label"></label>  
    44.             <div class="col-md-10">  
    45.                 <input asp-for="ProvinceState" class="form-control" />  
    46.                 <span asp-validation-for="ProvinceState" class="text-danger" />  
    47.             </div>  
    48.         </div>  
    49.         <div class="form-group">  
    50.             <label asp-for="ZipPostalCode" class="col-md-2 control-label"></label>  
    51.             <div class="col-md-10">  
    52.                 <input asp-for="ZipPostalCode" class="form-control" />  
    53.                 <span asp-validation-for="ZipPostalCode" class="text-danger" />  
    54.             </div>  
    55.         </div>  
    56.         <div class="form-group">  
    57.             <label asp-for="Country" class="col-md-2 control-label"></label>  
    58.             <div class="col-md-10">  
    59.                 <input asp-for="Country" class="form-control" />  
    60.                 <span asp-validation-for="Country" class="text-danger" />  
    61.             </div>  
    62.         </div>  
    63.         <div class="form-group">  
    64.             <label asp-for="ContactNumber" class="col-md-2 control-label"></label>  
    65.             <div class="col-md-10">  
    66.                 <input asp-for="ContactNumber" class="form-control" />  
    67.                 <span asp-validation-for="ContactNumber" class="text-danger" />  
    68.             </div>  
    69.         </div>  
    70.         <div class="form-group">  
    71.             <label asp-for="Email" class="col-md-2 control-label"></label>  
    72.             <div class="col-md-10">  
    73.                 <input asp-for="Email" class="form-control" />  
    74.                 <span asp-validation-for="Email" class="text-danger" />  
    75.             </div>  
    76.         </div>  
    77.         <div class="form-group">  
    78.             <label asp-for="WebSite" class="col-md-2 control-label"></label>  
    79.             <div class="col-md-10">  
    80.                 <input asp-for="WebSite" class="form-control" />  
    81.                 <span asp-validation-for="WebSite" class="text-danger" />  
    82.             </div>  
    83.         </div>  
    84.         <div class="form-group">  
    85.             <div class="col-md-offset-2 col-md-10">  
    86.                 <input type="submit" value="Update" class="btn btn-default" />  
    87.             </div>  
    88.         </div>  
    89.     </div>  
    90. </form>  
    91. <p>  
    92.     <a asp-action="Index">Back to List</a>  
    93. </p>  
    1. <td>  
    2.    <a asp-controller="Contact" asp-action="GetContact" asp-route-id="@item.ContactId">Get Details</a> |   
    3.    <a asp-controller="Contact" asp-action="UpdateContact" asp-route-id="@item.ContactId">Edit Details</a>  
    4. </td>  
    code

Run Application in Debug Mode

  • Press F5 or Debug Menu >> Start Debugging or click IIS Express on Toolbar to start application in debugging mode.
  • It will show Home Page in browser.
  • Click Contact List menu "Open" to open the Contact List Page.
  • Click "Edit Details" link to open Update Contact Page and the URL will change to http://localhost:21840/Contact/UpdateContact/3.
  • Edit contact data and click Update Button and it will update contact in the list.

    code

Sample Source Code

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