AngularJS And ASP.NET MVC Movie Library Application - Part Two

Hope you have had a chance to look into last the tutorial here which states about the technology stack and a other few vital keywords and terminology. Moving ahead in this article we’ll focus on adding a html page and will create module and controller as well as $scope element. Let’s get our hands dirty with AngularJS & ASP.NET MVC Movie Library Application Tutorials part two.

This is the technology stack for this Movie library application as shown below,

stack

Kindly create a MVC application as I have created and name it as per your need, for now I have also selected test project because we may write some TDD code later if needed though it is not essential. I’ve also added Application folder as per my need.

folder

After creating go to sample project, kindly go to NuGet package manager and choose AngularJS from there as shown in the below screen shots.

NuGet package manage

NuGet package manage

Go to the application folder and add an html page and named it Movie.html as shown in solution structure also. Add the following code in your respective page (movie.html) to make it function.

Code segment Movie.html
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.   
  5.     <h2>Movie Rating Portal</h2>  
  6.     <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  
  7.     <script src="../Scripts/angular.js"></script>  
  8.       
  9.      
  10.     <style>  
  11.         .selected  
  12.         {  
  13.             background-color: lightyellow;  
  14.             color: red;  
  15.             font-weight: bold;  
  16.         }  
  17.     </style>  
  18. </head>  
  19. <body ng-app="routingApp" class="jumbotron">  
  20.     <div ng-controller="MovieController" class="container">  
  21.         <div ng-show="success" class="alert-success">Record has been upddated for movie :  {{updatedMovie.title}}</div>  
  22.         <div class="row col-md-8">  
  23.             <table class="table table-striped ">  
  24.                  
  25.                 <thead class="thead-inverse">  
  26.                     <th style="background-color: Highlight">Title</th>  
  27.                     <th style="background-color: Highlight">Year of Release</th>  
  28.                     <th style="background-color: Highlight">Rating</th>  
  29.                     <th style="background-color: Highlight">Plot</th>  
  30.                     <th style="background-color: Highlight">Actions</th>  
  31.                 </thead>  
  32.                 <tr>  
  33.                     <!-- <td>EmployeeID:</td>-->  
  34.                     <td>  
  35.                         <input type="text" ng-model="movie.title" class="form-control" />  
  36.   
  37.                     </td>  
  38.                     <td>  
  39.                         <input type="text" ng-model="movie.year" class="form-control" />  
  40.                     </td>  
  41.                     <td>  
  42.                         <input type="text" ng-model="movie.rating" class="form-control" />  
  43.                     </td>  
  44.                     <td>  
  45.                         <input type="text" ng-model="movie.plot" class="form-control" />  
  46.                     </td>  
  47.                     <td>  
  48.                         <button type="button" class="btn btn-primary">  
  49.                             <span class="glyphicon glyphicon-plus"></span>  
  50.                         </button>  
  51.                     </td>  
  52.                 </tr>  
  53.                 <tbody>  
  54.                     <tr ng-repeat="movie in movies" ng-class="{'selected':$index == selectedRow}">  
  55.                         <td>{{movie.title}}  
  56.                         </td>  
  57.                         <td>{{movie.year}}  
  58.                         </td>  
  59.                         <td>{{movie.rating}}  
  60.                         </td>  
  61.                         <td>{{movie.plot }}  
  62.                         </td>  
  63.                         <td>  
  64.                             <button type="button" class="btn btn-warning">  
  65.                                 <span class="glyphicon glyphicon-pencil"></span>  
  66.                             </button>  
  67.                             <button type="button" class="btn btn-danger">  
  68.                                 <span class="glyphicon glyphicon-remove"></span>  
  69.                             </button>  
  70.                         </td>  
  71.                     </tr>  
  72.                 </tbody>  
  73.             </table>  
  74.         </div>  
  75.     </div>  
  76. </body>  
  77. </html>  
Note : Kindly add the reference of bootstrap as given below on movie.html partial template file.
  1. <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  
We are almost done with the UI part and it’s time to create Module and a controller to make .html fully functional. Add a .js file too --  I’ve added routing.js file and added the following code segment in that.
  1. /// <reference path="../Scripts/angular.js" />  
  2. var routingApp = angular.module('routingApp', []);  
  3. routingApp.controller("MovieController", ['$scope', function ($scope) {  
  4.   
  5.     $scope.edit = false;  
  6.     $scope.message = "Welcome to DotnetPiper.com to learn Angular";  
  7.     $scope.error = false;  
  8.     $scope.success = false;  
  9.     // alert("Servcie Called");  
  10.   
  11.     var movies = [  
  12.                 { title: "Revenent", year: "2015", rating: "5Star", plot: " A revenger Journey" },  
  13.                  { title: "Counjouring2", year: "2016", rating: "4Star", plot: " A Complete Hourror" },  
  14.                  { title: "DDLJ", year: "1995", rating: "SuperStar", plot: "Romantic love story" },  
  15.                  { title: "Sultan", year: "2016", rating: "5Star", plot: "A Warrior" },  
  16.                  { title: "BajiRao Mastani", year: "2015", rating: "4.5 Star", plot: "Film of the Year" }  
  17.     ];  
  18.   
  19.     $scope.movies = movies;  
  20. }]);  
Note : Module is like a namespace which has various classes like controller. Each module may have many controllers, and each controller can be bind to divtable or other html element. Once you have bound controller to some element then you can access each property of controller using $scope within that element scope.

Refer to given below image for reference,
reference
If you look at the image given below it is the Movie page which brings data from controller. For this purpose we have movie.html page which had a few controls. To beautify html controls I’ve added twitter bootstrap on each as permy need.

One you run an application give the path of your desired page. It should look like as given below in screen shot. For now button would not be working. That we’ll cover inthe next part.

application

Hope it’ll help you some day. Enjoy Coding.


Similar Articles