Angular, Bootstrap And ASP.NET MVC - Part Seven (Bootstrap Modal Forms)

In previous articles, we have learned AngularJS project setup, styling with bootstrap, AngularJS structure, data-binding, routing, copying of Angular object when form is saved or cancelled, inserting/updating forms, etc. Please have a look at my previous articles before going through this article.

From the previous articles, we have the following home page ready.

ASP.NET
When we click on this “Add new student” button, it opens Student Form as below, where empty form is shown and we can fill the data.

ASP.NET

In this article, we are going to learn bootstrap modal to open the form in a pop up. The modal is a dialog/popup window that is displayed on top of the current page. We will open the modal when “Add new student” button is clicked and display the same form in the modal.

Step 1

We need to install “Angular JS UI Bootstrap”. So, from the NuGet Package Manager, install “Angular.UI.Bootstrap”.

ASP.NET

Step 2

Let’s include this Angular UI bootstrap file in the “index.html”. We are using “ui-bootstrap-tpls.min.js” because it provides lots of bootstrap controls.

ASP.NET

Step 3

In “StudentFormsApp.js”, we need dependency on ‘ui.bootstrap’, so update your module with this command.

  1. var studentFormsApp = angular.module('studentFormsApp', ["ngRoute""ui.bootstrap"]);  

Step 4

In the “HomeController.js” file, let's add “$uibModal” service, so it will look like -

  1. studentFormsApp.controller("homeController",   
  2.     function ($scope, $location, $uibModal) {  
  3.         $scope.addNewStudent = function () {  
  4.             $uibModal.open({  
  5.                 templateUrl: "app/StudentForm/sfTemplate.html",  
  6.                 controller: "sfController"   
  7.             });  
  8.   
  9.         };  
  10.         $scope.updateStudent = function (id) {  
  11.             $location.path('/updateStudentForm/'+ id);    
  12.         };  
  13.     })  

In the above code snippet, we are opening modal in click event of “Add new student”.

  1. $uibModal.open({  
  2.      templateUrl: "app/StudentForm/sfTemplate.html",  
  3.      controller: "sfController"   
  4. });  

We are passing “templateUrl” and “controller” as options when opening the modal.

Step 5

Next, for the good layout of pop up dialog, we need to wrap up our html with classes “modal-header”, “modal-title”, “modal-footer”. We need to include these classes to make UI better.

So, in “sfTemple.html”

ASP.NET

Final “sfTemplate.html” will look like the following.

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

Step 6

Run the application.

When you click on the “Add new student”, then it will display a pop-up dialog as below.

ASP.NET

Get the complete project from GitHub.

In this article, we have learned how to generate Bootstrap pop-up dialog. We will learn more in the next articles.

Thanks, Happy coding!


Similar Articles