Simple Login Form In AngularJS & MVC

Step 1

Click New Project in the Start page or in the File menu.

In the New Project dialog, click Web in the left pane and Web Application in the middle pane.

Step 2

Right click on controller folder > add > click on controller > now enter the name of the controller.

Select Empty MVC controller > Add new controller into controller folder.

Follow the image given below.

Step 3

Right click on controller folder > add > click on controller >add->class for LoginController.

 

Step 4

Install Angularjs package. Here, I have explained about installing AngularJS package. You can download AngularJS.

You can add the project solution on Visual Studio. Follow the steps......Go to Solution Explorer> right click on the project solution > click on manage NuGet package > search text online "angularjs" > click to Install.

 

 

Step 5

Add three new JavaScript files (module.js, controller.js and Service.js) on "AngulrJs" folder under Script folder.

Go to solution explorer > Right click on "Angularjs" folder under Script folder > add > select JavaScript file > enter the name of the file > Add ->Now, lets move on. Write the code given below for the particluar file.

LoginController & LoginService in JS file creation.

Step 6

Now, the time has come to set up Angular JS Module, Service and Controller. Thus, create a folder with name “Angular” inside the Content folder for the clean code.

Afterwards, add three JavaScript files inside "Angular" folder create.

  1. module.js
  2. Loginservice.js
  3. Logincontroller.js

module.js

We will first initialize and register our Application by creating a new Angular module.

var LoginApp = angular.module(' MyApp ',[]);

A module is a collection of Services, Directives, Controllers, filters, and configuration information.

Logincontroller.js

controller.js file is use to show the data on the view.

  1. LoginApp.controller('LoginController', ['$scope'' MyService 'function($scope, MyService) {  
  2.     $scope.submit = function() {  
  3.         var UserData = {  
  4.             Email: $scope.Email,  
  5.             Password: $scope.Password,  
  6.         }  
  7.         MyService.LoginValidate(UserData).then(function(pl) {  
  8.             //result data  
  9.             alert(pl.data);  
  10.         });  
  11.     }  
  12. }]);  

Loginservice.js

This JavaScript file "factory.js" for fetching the data from the database and provide it to the controller.

  1. angular.module('LoginApp').service('LoginService'function($http) {  
  2.     this.LoginValidate = function(UserData) {  
  3.         var result = $http({  
  4.             method: "Post",  
  5.             url: --XXXXXXXXXXXXXXXXXXX’ //pass UserData values to class file  
  6.             data: save  
  7.         });  
  8.         return result;  
  9.     }  
  10. });  

Step 7

Add new action into Controller to get view from login, in which I have added "LoginView" action into Logincontroller. Please write the code given below.

Add class Logincontoller add values to the database to send & verify the data.

  1. public ViewResult LoginForm() {  
  2.     return View();  
  3. }  
  4. //pass and check tha value  
  5. public JsonResult Loginvalidate(Userdata) {  
  6.         HttpClient client = new HttpClient();  
  7.         string _url = ConfigurationManager.AppSettings["webapibaseurl"];  
  8.         client.BaseAddress = new Uri(_url);  
  9.         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));  
  10.         MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();  
  11.         HttpResponseMessage response = client.GetAsync(“URL”).Result;  
  12.         string content1 = response.Content.ReadAsStringAsync().Result;  
  13.         if (response.StatusCode.ToString() == "OK") {  
  14.             return Json(content1, JsonRequestBehavior.AllowGet);  
  15.         }  

Step 8 

Add view for "LoginView" Action

Right click on Index action method > Add view > Enter view name > Add ->LoginForm
 
  
  1. <!DOCTYPE html>  
  2. <html>  
  3.   
  4. <head>  
  5.     <title>Login</title>  
  6.     <script src="~/Scripts/angular.min.js"></script>  
  7.     <script src="~/Scripts/Angular/Module.js"></script>  
  8.     <script src="~/Scripts/Angular/Logincontroller.js"></script>  
  9.     <script src="~/Scripts/Angular/Loginservice.js"></script>  
  10. </head>  
  11.   
  12. <body ng-app=" LoginApp ">  
  13.     <div ng-controller="LoginController">  
  14.         <form name="loginform">  
  15.             <div style="color:green">{{message}}</div>  
  16.             <table>  
  17.                 <tr>  
  18.                     <td>User Name</td>  
  19.                     <td><input type="text" ng-model="Email" name="Email" /></td>  
  20.                 </tr>  
  21.                 <tr>  
  22.                     <td>Password</td>  
  23.                     <td><input type="text" ng-model="Password" name="Pin" /></td>  
  24.                 </tr>  
  25.                 <tr>  
  26.                     <td></td>  
  27.                     <td><input type="submit" ng-click="submit()" /></td>  
  28.                 </tr>  
  29.             </table>  
  30.         </form>  
  31.     </div>  
  32. </body>  
  33.   
  34. </html>   

Step 9

Add new action into controller, which helps in redirecting to the other page. After user login, I have added "Index" action into Logincontroller. Please write the code given below. 

  1. public ActionResult Index() {  
  2.     if (Session["Email"] != null) {  
  3.         return View();  
  4.     } else {  
  5.         return RedirectToAction("LoginView");  
  6.     }  
  7. }   

Step 10 

Add a view for Action

This view page can display after user login. Please write the code given below.

Up Next
    Ebook Download
    View all
    Learn
    View all