AngularJS, Bootstrap And ASP.NET MVC - Part Five (Saving/ Cancelling Form)

In previous articles, we have learned AngularJS project setup, styling with bootstrap, AngularJS structure, data-binding, routing, etc. Please have a look at my previous articles before going through this article.

In this tutorial, we are going to learn how we can save the form data when the "Save" button is clicked and reset the form when "Cancel" button is clicked.

Step 1

Have a look in “sfTemplate.html” from previous articles (Part – 4) where we have a submit button. Let’s add "Cancel" button beside this Submit button and add respective click events (submitForm() and cancelForm()).

  1. <div class="col-sm-offset-3 col-sm-9">  
  2.     <input type="button" class="btn btn-default" value="Cancel" ng-click="cancelForm()" />  
  3.     <input type="submit" class="btn btn-primary" value="Submit" ng-click="submitForm()" />  
  4. </div>  We have implemented routing in the previous article so when you run the application, at first it will show home page with “Add new student” button as below.


AngularJS
When you click on that button, you will be redirected to “sfTemplate.html”. You can see two buttons at the bottom of the page as below.

AngularJS
Step 2

In the “sfController.js”, let's add respective click events.

  1. $scope.submitForm = function () {  
  2.     alert("Form submitted");  
  3. }  
  4. $scope.cancelForm = function () {  
  5.     alert("Form cancelled");  
  6. }  

Step 3

Let’s make some change in the form data and click on "Submit" button. Now, go back to the home page. When we go into the form page again, the page gets restored with the latest data. Data has been saved in the “$scope” since we have 2-way data binding implementation.

When we click on the "Cancel" button, it should reset the data but in this case, it is not resetting. So, when we click on the "Cancel" button, any changes made in the form should be reverted. The edited value is stored in the “$scope”.

This works fine while submitting the form but not good when cancelling the form.

To avoid this, we can create a copy of “student” variable. When we submit the form, then we will copy this updated variable back into “student” variable and when we cancel the form, we will do nothing.

This is our “sfController.js” code snippet.

  1. studentFormsApp.controller('sfController',  
  2.     function sfController($scope, sfService) {  
  3.         $scope.student = sfService.student;  
  4.   
  5.         $scope.departments = [  
  6.                "Math",  
  7.                "Physics",  
  8.                "Chemistry",  
  9.                "English"  
  10.         ];  
  11.         $scope.submitForm = function () {  
  12.             alert("Form submitted");  
  13.         }  
  14.         $scope.cancelForm = function () {  
  15.             alert("Form cancelled");  
  16.         }  
  17.     });  

Step 4

AngularJS provides an easy way to copy an object.  Let’s create an object with name “updatedStudent” and call “angular.copy” passing “$scope.student”.

  1. $scope.updatedStudent = angular.copy($scope.student);  

When we submit the form, we just copy it back to “$scope.student”. So, inside the click event of submit button, we will copy “$scope.updatedStudent” to “$scope.student” object like -

  1.  $scope.submitForm = function () {  
  2.     $scope.student = angular.copy($scope.updatedStudent);  
  3. }  

Here, we are updating “$scope.student” with the new data that is stored in “$scope.updatedStudent”.

Step5

Next, we need to update with the “updatedStudent” where there is “student” variable. So, in “sfTemplate.html”, replace “student” with “updatedStudent” in every ng-model.

Complete code snippet of “sfController.js” will look like,

  1. studentFormsApp.controller('sfController',  
  2.     function sfController($scope, $window ,sfService) {  
  3.         $scope.student = sfService.student;  
  4.   
  5.         $scope.updatedStudent = angular.copy($scope.student);  
  6.   
  7.         $scope.departments = [  
  8.                "Math",  
  9.                "Physics",  
  10.                "Chemistry",  
  11.                "English"  
  12.         ];  
  13.         $scope.submitForm = function () {  
  14.             $scope.student = angular.copy($scope.updatedStudent);  
  15.             $window.history.back();  
  16.         }  
  17.         $scope.cancelForm = function () {  
  18.             $window.history.back();  
  19.         }  
  20.     });  

Here, we have added code for going back to home page on click of both, Submit and Cancel buttons. The main idea is that we have saved updated form data ($scope.updatedStudent) in another “$scope.student” variable. This helps to save the form data when Submit button is clicked and reverted back to original data when Cancel button is clicked.

New “sfTemplate.html” with “ng-model” updated with “updatedStudent”

  1. <form role="form" class="form-horizontal">  
  2.     <fieldset>  
  3.         <legend>Basic Information</legend>  
  4.         <div class="form-group">  
  5.             <label for="fullName" class="col-sm-3 control-label">Name</label>  
  6.             <div class="col-sm-9">  
  7.                 <input type="text" id="fullName" name="fullName" class="form-control"  
  8.                        ng-model="updatedStudent.fullName" />  
  9.             </div>  
  10.         </div>  
  11.         <div class="form-group">  
  12.             <label for="objective" class="col-sm-3 control-label">Objective</label>  
  13.             <div class="col-sm-9">  
  14.                 <textarea name="objective" id="objective" class="form-control" rows="5" cols="40" ng-model="updatedStudent.objective"></textarea>  
  15.             </div>  
  16.         </div>  
  17.   
  18.         <div class="form-group">  
  19.             <label for="department" class="col-sm-3 control-label">Department</label>  
  20.             <div class="col-sm-9">  
  21.                 <select name="department" id="department" class="form-control"  
  22.                         ng-model="updatedStudent.department"  
  23.                         ng-options="dept for dept in departments"></select>  
  24.             </div>  
  25.         </div>  
  26.     </fieldset>  
  27.   
  28.     <br />  
  29.   
  30.     <fieldset>  
  31.         <legend>Hobbies</legend>  
  32.         <div class="form-group">  
  33.             <div class="col-sm-3">  
  34.   
  35.             </div>  
  36.             <div class="col-sm-9">  
  37.                 <div class="checkbox">  
  38.                     <label><input type="checkbox" value="hobbiesTravel" ng-model="updatedStudent.hobbiesTravel" />Travelling</label>  
  39.                 </div>  
  40.                 <div class="checkbox">  
  41.                     <label><input type="checkbox" value="hobbiesPhotography" ng-model="updatedStudent.hobbiesPhotography" />Photography</label>  
  42.                 </div>  
  43.                 <div class="checkbox">  
  44.                     <label><input type="checkbox" value="hobbiesGaming" ng-model="updatedStudent.hobbiesGaming" />Gaming</label>  
  45.                 </div>  
  46.                 <br />  
  47.             </div>  
  48.         </div>  
  49.     </fieldset>  
  50.   
  51.     <fieldset>  
  52.         <legend>Gender</legend>  
  53.         <div class="form-group">  
  54.             <div class="col-sm-3"></div>  
  55.             <div class="col-sm-9">  
  56.                 <div class="radio">  
  57.                     <label><input type="radio" name="gender" value="Male" ng-model="updatedStudent.gender" /> Male</label><br />  
  58.                 </div>  
  59.                 <div class="radio">  
  60.                     <label><input type="radio" name="gender" value="Female" ng-model="updatedStudent.gender" /> Female</label><br />  
  61.                 </div>  
  62.                 <br />  
  63.             </div>  
  64.         </div>  
  65.     </fieldset>  
  66.     <div class="col-sm-offset-3 col-sm-9">  
  67.         <input type="button" class="btn btn-default" value="Cancel" ng-click="cancelForm()" />  
  68.         <input type="submit" class="btn btn-primary" value="Submit" ng-click="submitForm()" />  
  69.     </div>  
  70. </form>   

Get the complete project from GitHub.

We learned saving of form data in AngularJS. We will continue to learn more in the next articles.

Up Next
    Ebook Download
    View all
    Learn
    View all