Angular, Bootstrap And ASP.NET MVC - Part Six

In previous articles, we have learnt AngularJS project setup, styling with bootstrap, AngularJS structure, data-binding, routing, and copying of Angular objects when form is saved or cancelled, etc.

Please have a look at my previous articles before going through this article.

From the previous articles, we have the following homepage.

ASP.NET

When we click on this “Add new student” button, then it will navigate to Student Form as below, where form data is shown.

ASP.NET

In this article, we are going to manage the same page for both options, updating the existing data and also inserting new data.

Step 1

In “Home.html”, let's add another button that updates the existing student data when clicked. So, “Home.html” will look like the following.

  1. <input type="button" class="btn btn-primary" value="Add new student" ng-click="addNewStudent()" />  
  2.    <br />  
  3.    <br/>  
  4. <input type="button" class="btn btn-primary" value="Update student" ng-click="updateStudent(1)"/>  

For testing purposes, we are passing hard-coded id (1) in “updateStudent” method. In real applications, we get id from the list and pass it like this “ng-click="updateStudent(id)"”.
ASP.NET
Step 2

Let’s create respective methods in “HomeController.js”.

  1. studentFormsApp.controller("homeController",   
  2.     function ($scope, $location) {  
  3.         $scope.addNewStudent = function () {  
  4.             $location.path('/newStudentForm');  
  5.         };  
  6.   
  7.         $scope.updateStudent = function (id) {  
  8.             $location.path('/updateStudentForm/'+ id);  /*Here we are passing id. For 'updateStudentForm' we need to add route in StudentFormsApp.js*/  
  9.         };  
  10.     })  

In “updateStudent” function, we are passing “id”. We are linking path to ‘updateStudentForm’ so we need to set up route in “StudentFormsApp.js”.

Step 3

In “StudentFormsApp.js”, we will add new “.when” method that links to “sfTemplate.html” but with “id” as parameter.

  1. .when("/updateStudentForm/: id", {  
  2.         templateUrl: "app/StudentForm/sfTemplate.html",  
  3.         controller: "sfController"  
  4.     })  

Step 4

Please note that we are opening the same “sfTemplate.html” and same Controller “sfController”. But within this Controller, we need to check if the function has “id” as parameter. If “id” is passed then it means the form with previous data should be displayed whereas if “id” is not passed, it means form with empty data should be displayed.

So, in “sfController.js”, lets add “$routeParams” as the service which holds the parameters “id” in this case. So, we check if “$routeParams” has “id” or not.

Final code for “sfController.js” will look like,

  1. studentFormsApp.controller('sfController',  
  2.     function sfController($scope, $window, $routeParams, sfService) {  
  3.          
  4.  if ($routeParams.id) {  
  5.             $scope.student = sfService.getStudent($routeParams.id);  
  6.         }  
  7.         else {  
  8.             $scope.student = { id: 0 };  
  9.         }  
  10.   
  11.   
  12.         $scope.updatedStudent = angular.copy($scope.student);  
  13.   
  14.         $scope.departments = [  
  15.                "Math",  
  16.                "Physics",  
  17.                "Chemistry",  
  18.                "English"  
  19.         ];  
  20.   
  21.         $scope.submitForm = function () {  
  22.             if ($scope.updatedStudent.id == 0) {  
  23.                 //insert new student data  
  24.                 sfService.insertStudent($scope.updatedStudent);  
  25.             }  
  26.             else {  
  27.                 //update the student data  
  28.                 sfService.updateStudent($scope.updatedStudent);  
  29.             }  
  30.   
  31.             $scope.student = angular.copy($scope.updatedStudent);  
  32.             $window.history.back();  
  33.         }  
  34.         $scope.cancelForm = function () {  
  35.             $window.history.back();  
  36.         }  
  37.     });  

From the above code, if we have passed “id” as parameter then it will retrieve the data from “sfService” as “sfService.getStudent($routeParams.id)”

In the “submitForm”, if we have “id == 0”, this means it is new student’s data so we need to insert this like,

  1. sfService.insertStudent($scope.updatedStudent);  

Whereas, if it has “id != 0”, this means we have to update its data so we are updating like,

  1. sfService.updateStudent($scope.updatedStudent);  

Step 5

Next, we need to create ‘getStudent’, ‘insertStudent’ and ‘updateStudent’ methods in “sfService.js” so final code snippet of “sfService.js” will look like,

  1. studentFormsApp.factory('sfService',  
  2.     function () {  
  3.         var getStudent = function (id) {  
  4.             if (id == 1) {  
  5.                 return {  
  6.                     id: 1,  
  7.                     fullName: "Kishor Bikram Oli",  
  8.                     objective: "To become a great software engineer",  
  9.                     department: "Physics",  
  10.                     hobbiesGaming: true,  
  11.                     hobbiesTravel: false,  
  12.                     hobbiesPhotography: true,  
  13.                     gender: "Male"  
  14.                 };  
  15.             }  
  16.             return null;  
  17.         };  
  18.   
  19.         var insertStudent = function (newStudent) {  
  20.             // Webservice, ajax calls to insert into the database  
  21.             return true;  
  22.         };  
  23.   
  24.         var updateStudent = function (student) {  
  25.             //Webservice, ajax calls to updated into the database  
  26.             return true;  
  27.         };  
  28.         return {  
  29.             getStudent: getStudent,  
  30.             insertStudent: insertStudent,  
  31.             updateStudent: updateStudent,  
  32.         };  
  33.     });  

From the above code, in the “getStudent” method, if the id == 1 then we are returning our data else returning null. This ensures that if “id” is passed then it will return respective data and display this data in the form otherwise, it will display form with empty data.

Step 6

Final code of “StudentFormsApp.js” will look like,

  1. var studentFormsApp = angular.module('studentFormsApp', ["ngRoute"]);  
  2.   
  3. studentFormsApp.config(function ($routeProvider) {  
  4.     $routeProvider  
  5.         .when("/home", {  
  6.             templateUrl: "app/Home.html",  
  7.             controller: "homeController"  
  8.     })  
  9.     .when("/newStudentForm", {  
  10.         templateUrl: "app/StudentForm/sfTemplate.html",  
  11.         controller: "sfController"  
  12.     })  
  13.     .when("/updateStudentForm/:id", {  
  14.         templateUrl: "app/StudentForm/sfTemplate.html",  
  15.         controller: "sfController"  
  16.     })  
  17.     .otherwise({  
  18.         redirectTo: "/home"  
  19.     })  
  20. });  

Step 7

Run the application.

When you click on the “Add new student” then it will display form with empty data.

ASP.NET

And when you click on “Update Student” button, then it will display the same form but with student data.

ASP.NET

Get the complete project from GitHub.

We will learn more in the next articles. I'll keep you posted.

Thanks, Happy coding!


Similar Articles