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

Hope you got a chance to look into my last tutorials about technology stack, creating UI HTML page with Modules and Controllers, and Event Binding with UI elements. Kindly, find the links here:

Moving ahead, in this article, we’ll focus on integration of movie.html page with MVC layout page. Also, we’ll implement Angular routing in that.

Let’s get our hands busy with AngularJS & ASP.NET MVC Movie Library Application - Part Four.

So far, we’ve designed the following screen shot:

portal

For this purpose, I’ve created a few more pages so that we can implement routing easily. Kindly have a look at the image below, for reference.

reference
So, please create other pages as well. Probably after Part 6 of Movie Library Application, I’ll share my application with you for downloading.

Open the Solution Explorer and look for _Layout.cshtml page there. You can give reference of main.js (in our case it is routing.js) file as a reference there also, so that it would be applicable for all .cshtml pages.This is because _Layout page loads first in MVC life cycle.

Routing.js file contains all controllers' code used in all html pages e.g. Movie.html, searchMovie.html and tollywood.html.

It works in such a manner that as soon as the _layout.cshtml page loads, it also loads Module along with all respective controllers defined under this JavaScript file.

Kindly, find the code below for how to bind routing.js file.

code
If you have noticed in the above screenshot, I’ve set the menu using bootstrap which looks like the following screen, now:

screen

Understand ng_view: Another important factor is the usage of ng-view directive. In our angular app, we need to define ng-app directive once. Ng-view becomes the placeholder for views. Each view referred by the route is loaded in this section of the document which we have defined on _layout.cshtml. You can also define ng-view in main html file .There are a few ways to define it, as shown below. I’ve chosen <ng-view></ng-view> tag.

For my purpose, kindly refer to the above screen shot for reference and look at bottom of image.

  1. <div ng-view></div>  
  2. <ng-view></ng-view>  
  3. <div class="ng-view"></div>  
Kindly, find the code below for_layout.cshtml, so that you may fit into your code segment also.

Code snippet _layout.cshtml
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <title>@ViewBag.Title - My ASP.NET MVC Application</title>  
  6.     <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />  
  7.     <meta name="viewport" content="width=device-width" />  
  8.     @Styles.Render("~/Content/css")  
  9.     @Scripts.Render("~/bundles/modernizr")  
  10.     <script src="~/Scripts/angular.min.js"></script>  
  11.   
  12.     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular-route.js"></script>  
  13.     @*    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>*@  
  14.     <script src="~/Application/App.js"></script>  
  15.     <script src="~/Application/Routing.js"></script>  
  16.   
  17. </head>  
  18. <body>  
  19.     <header>  
  20.         <div class="content-wrapper">             
  21.             <div class="float-right">  
  22.                 <section id="login">                     
  23.                 </section>  
  24.                 <nav>                     
  25.                 </nav>  
  26.             </div>  
  27.         </div>  
  28.     </header>  
  29.     <div id="body" ng-app="routingApp" class="float-center">  
  30.         <nav class="navbar navbar-inverse navbar-fixed-top">  
  31.             <div class="container-fluid">  
  32.                 <div class="navbar-header">  
  33.                     <a class="navbar-brand" href="www.dotnetpiper.com">DotnetPiper.Com</a>  
  34.                 </div>  
  35.                 <ul class="nav navbar-nav">  
  36.                     <li class="active"><a href="#/Home"><b>Home</b></a></li>  
  37.                     <li><a href="#/Movie"><b>Bollywood</b></a></li>  
  38.                     <li><a href="#/SearchMovie"><b>Seacrh Movie Globally</b></a></li>  
  39.                     <li><a href="#/Tolly"><b>Tollywood</b></a></li>  
  40.                 </ul>  
  41.                 <ul class="nav navbar-nav navbar-right">  
  42.                     <li><a href="#"><span class="glyphicon glyphicon-user"></span>Sign Up</a></li>  
  43.                     <li><a href="#"><span class="glyphicon glyphicon-log-in"></span>Login</a></li>  
  44.                 </ul>  
  45.             </div>  
  46.         </nav>  
  47.   
  48.         <ng-view></ng-view>  
  49.         @RenderSection("featured", required: false)  
  50.   
  51.         @RenderBody()  
  52.   
  53.     </div>  
  54.     @* <footer>  
  55.         <div class="content-wrapper">  
  56.             <div class="float-left">  
  57.                 <p>© @DateTime.Now.Year - My ASP.NET MVC Application</p>  
  58.             </div>  
  59.         </div>  
  60.     </footer>*@  
  61.   
  62.     @Scripts.Render("~/bundles/jquery")  
  63.     @RenderSection("scripts", required: false)  
  64. </body>  
  65. </html>  
Add Routing in AngularJS

We’ve included a JavaScript file, routing.js, which holds the application logic. Given below is the content of routing.js. Kindly ensure that you have given the reference for route.js, as shown below:
  1. /// <reference path="../Scripts/angular-route.js" />  
  2.   
  3. var routingApp = angular.module('routingApp', ['ngRoute']);  
  4.   
  5. routingApp.config(['$routeProvider'function ($routeProvider) {  
  6.     //alert("Route Initiated");  
  7.     $routeProvider.  
  8.         when('/Home', { templateUrl: '/Application/login.html', controller: '/Application/testController' }).  
  9.         when('/Movie', { templateUrl: '/Application/Movie.html', controller: 'MovieController' }).  
  10.         when('/SearchMovie', { templateUrl: '/Application/SearchMovie.html', controller: 'ApiMovieController' }).  
  11.          when('/Tolly', { templateUrl: '/Application/Tollywood.html', controller: 'tollyController' }).  
  12.         otherwise({ redirectTo: '/' });  
  13. }]);  
First, use .config() method to define $routeProvider configuration. The routeProvider identifies the route, e.g., whenever there will be a string “/Movie” in route URL, it should load template "/Application/Movie.html" from partial templates. (I’m calling these .html pages as partial pages).

After implementing the Routing, it should work in a way, as depicted below. Kindly notice the URLchanging on each click.

output

This is the complete code for routing.js which is main app file.
  1. /// <reference path="../Scripts/angular.js" />  
  2. /// <reference path="../Scripts/fusioncharts.js" />  
  3. /// <reference path="../Scripts/fusioncharts.charts.js" />  
  4. /// <reference path="../Scripts/angular-route.js" />  
  5.   
  6. var routingApp = angular.module('routingApp', ['ngRoute']);  
  7.   
  8. routingApp.config(['$routeProvider'function ($routeProvider) {  
  9.     //alert("Route Initiated");  
  10.     $routeProvider.  
  11.         when('/Home', { templateUrl: '/Application/login.html', controller: '/Application/testController' }).  
  12.         when('/Movie', { templateUrl: '/Application/Movie.html', controller: 'MovieController' }).  
  13.         when('/SearchMovie', { templateUrl: '/Application/SearchMovie.html', controller: 'ApiMovieController' }).  
  14.          when('/Tolly', { templateUrl: '/Application/Tollywood.html', controller: 'tollyController' }).  
  15.         otherwise({ redirectTo: '/' });  
  16. }]);  
  17.   
  18. routingApp.controller("tollyController"function ($scope) {  
  19.   
  20.     $scope.tollyMessage = "Welcome to TollyWood to watch Action,Thriller and Suspence movies";  
  21.   
  22. });  
  23.   
  24.   
  25. routingApp.controller("MovieController", ['$scope'function ($scope) {  
  26.   
  27.     $scope.edit = false;  
  28.     $scope.message = "Welcome to DotnetPiper.com to learn Angular";  
  29.     $scope.error = false;  
  30.     $scope.clear = false;  
  31.     $scope.success = false;  
  32.     // alert("Servcie Called");  
  33.   
  34.     var movies = [  
  35.                 { title: "Revenent", year: "2015", rating: "5Star", plot: " A revenger Journey" },  
  36.                  { title: "Counjouring2", year: "2016", rating: "4Star", plot: " A Complete Hourror" },  
  37.                  { title: "DDLJ", year: "1995", rating: "SuperStar", plot: "Romantic love story" },  
  38.                  { title: "Sultan", year: "2016", rating: "5Star", plot: "A Warrior" },  
  39.                  { title: "BajiRao Mastani", year: "2015", rating: "4.5 Star", plot: "Film of the Year" }  
  40.     ];  
  41.   
  42.     $scope.movies = movies;  
  43.   
  44.     $scope.AddMovie = function (movie) {  
  45.   
  46.         if ($scope.edit == true) {  
  47.   
  48.             var index = $scope.movies.indexOf(movie);  
  49.             // alert("edit Called");  
  50.             $scope.movies[index] = movie;  
  51.             //alert(movie.rating);  
  52.             $scope.updatedMovie = movie;  
  53.             $scope.success = true;  
  54.             $scope.movie = {};  
  55.         }  
  56.         else {  
  57.             var newMovie = {  
  58.                 title: $scope.movie.title,  
  59.                 year: $scope.movie.year,  
  60.                 rating: $scope.movie.rating,  
  61.                 plot: $scope.movie.plot  
  62.             };  
  63.   
  64.             movies.push(newMovie);  
  65.             // alert("Add Called");  
  66.         }  
  67.     }  
  68.   
  69.     $scope.DeleteMovie = function (movie, index) {  
  70.   
  71.         movies.splice(index, 1);  
  72.         // $scope.movie = movie;  
  73.         $scope.updatedMovie = movie;  
  74.         $scope.success = false;  
  75.         $scope.clear = true;  
  76.         $scope.movie = {};  
  77.         console.log(index);  
  78.     }  
  79.     $scope.EditMovie = function (movie, index) {  
  80.   
  81.         $scope.selectedRow = null;  // initialize our variable to null  
  82.         $scope.selectedRow = index;  
  83.         $scope.movie = movie;  
  84.         $scope.edit = true;  
  85.     }  
  86. }]);  
Hope it’ll help you some day. Enjoy Coding.


Similar Articles