Introduction
Create Database and Tables
  • Firstly Create database named TestData.
  • After creating the database create a table with the name Logins.
  • Set the Id column as primary key and auto increment id in every table.
  • That's it with the database.
Get the JS and CSS files from given links
To be familiar with the things Click Here

Let's start with the code
  • Open the VS2012 ,Create New ASP.Net MVC4 Web Application and give the Name LoginAngularMvcApp.
  • Go to Models Folder in the solution explorer of visual studio.
  • Right click the Models folder and find ADD >>> ADO.NET Entity Data Model. Select ADO.NET Entity Data Model.
  • Provide the name LoginModel. After that pop up will appear .


  • Select generate from database and click next.


  • In the given box type entity name as SahilEntities and After that click New connection.


  • Select Sql server authentication and fill the credentials like server name ,user name ,password,and then select your database from the database list.

  • Check the checkboxes of tables and click on finish.
Create the LoginController by right clicking on the controller folder
  • Change the Index ActionResult name from index to Login.This will return the Login View.
  • After that [HttpPost] Login method with Login class as parameter serves the purpose for actual operation of login.
  • Here we first check from the database that requesting user exists in the database or not.If exists than enteredpassword is compared with database password.
  • If exists return 0,-1 or userID.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using BootstrapThemeAngular.Models;
  7. using BootstrapThemeAngular.Utilities;
  8. using System.Text;
  9. using System.Web.Security;
  10. namespace BootstrapThemeAngular.Controllers
  11. {
  12. public class LoginController : Controller
  13. {
  14. // GET: /Login/
  15. public ActionResult Login()
  16. {
  17. return View();
  18. }
  19. [HttpPost]
  20. public string Login(Login data)
  21. {
  22. bool isPasswordCorrect = false;
  23. string un = data.Username;
  24. string Password = data.Password;
  25. using (SahilEntities entity = new SahilEntities())
  26. {
  27. var user = entity.Logins1.Where(u => u.UserName == un).FirstOrDefault();
  28. if (user != null)
  29. {
  30. if (Password == user.Password)
  31. {
  32. Session["LoginID"] = user.ID;
  33. Session["Username"] = user.Fname + ' ' + user.Lname;
  34. return user.ID.ToString();
  35. }
  36. else
  37. {
  38. return "0";
  39. }
  40. }
  41. else
  42. {
  43. return "-1";
  44. }
  45. }
  46. }
Add 3 files --> Module.js,Controller.js,Service.js in content folder
  • Here we register the module with the application.
  • Here myApp is the module name that we used with ng-app directive in HTML view.
  • In Module.js write the given code.
  1. var app = angular.module("myApp", []);
  • myCntrl is the controller name that is registered with the myApp module and used in HTML view with ng-controller directive.
  • $scope is used to refers to the application.Data passed to $scope in controller is accessible in view.
  • myService is the name of service that is used with controller to call the functions from server.
  • LoginCheck function get the username and password from $scope and store in the object variables.
  • UserLogin function from service is called that returns the response in form of values after that certain action performs.
  • Clearfields clear the HTML controls after login.
  • alertmsg hides the alert modal that displays alert messages.
  1. app.controller("myCntrl", function ($scope, myService) {
  2. $scope.LoginCheck = function () {
  3. var User = {
  4. UserName: $scope.uName,
  5. Password: $scope.password
  6. };
  7. $("#divLoading").show();
  8. var getData = myService.UserLogin(User);
  9. getData.then(function (msg) {
  10. if (msg.data == "0") {
  11. $("#divLoading").hide();
  12. $("#alertModal").modal('show');
  13. $scope.msg = "Password Incorrect !";
  14. }
  15. else if (msg.data == "-1") {
  16. $("#divLoading").hide();
  17. $("#alertModal").modal('show');
  18. $scope.msg = "Username Incorrect !";
  19. }
  20. else {
  21. uID = msg.data;
  22. $("#divLoading").hide();
  23. window.location.href = "/Home/Index";
  24. }
  25. });
  26. debugger;
  27. }
  28. function clearFields() {
  29. $scope.uName = '';
  30. $scope.uPwd = '';
  31. }
  32. });
  1. $scope.alertmsg = function () {
  2. $("#alertModal").modal('hide');
  3. };
  • In Service.js write the given code.
  • myService is the name of the service registers with the myApp module.
  • $http is passed as parameter. $http serves the purpose for ajax call to the server.
  • In this service UserLogin function is used to call the Login method from server by providing url.
  • This method returns the response to the controller.
  1. app.service("myService", function ($http) {
  2. this.UserLogin = function (User) {
  3. var response = $http({
  4. method: "post",
  5. url: "/Login/Login",
  6. data: JSON.stringify(User),
  7. dataType: "json"
  8. });
  9. return response;
  10. }
  11. });
Now write the code to display the view on screen
  • Write the code in Login.cshtml
  • In View HTML tag is used with the directive ng-app. ng-app calls the myApp module to initialize with this view.
  • After that all the css and script files are dropped in head tag.
  • ng-controller directive is used with the div tag to initialize with the controller we created.
  • <div class="container" ng-controller="myCntrl"> . Here ng-controller is a directive and myCntrl is the name of controller we specify in the controller.js file.
  • Alert modal is placed to display the messages for password or username incorrect.
  • In the container fluid class the panel is placed to put the controls on the panel.
  • CSS is applied to the panel for attractive look.
  • divLoading is used to just make the waiting time screen attractive.This serves for no other purpose.
  • Button placed after that to call login Method.Button contains ng-disabled directive ,use of this directive is untill,unless controls are not filled the Login button is disabled.
  • LoginCheck() function in ng-click directive calls the funtion from controller after click.
  1. @{
  2. ViewBag.Title = "Login";
  3. }
  4. <html ng-app="myApp">
  5. <head>
  6. <title></title>
  7. <link href="~/Content/bootstrap.min.css" rel="stylesheet" />
  8. <script src="~/Content/Angular/RegisterModule.js"></script>
  9. <script src="~/Content/Angular/RegisterService.js"></script>
  10. <script src="~/Content/Angular/RgstrController.js"></script>
  11. <script src="~/Content/Angular/dirPagination.js"></script>
  12. </head>
  13. <body>
  14. <div ng-controller="myCntrl">
  15. <h1>
  16. <img src="~/Content/images/Loginicon.png" /></h1>
  17. <br />
  18. <div id="alertModal" class="modal fade">
  19. <div class="modal-dialog">
  20. <div class="modal-content">
  21. <!-- dialog body -->
  22. <div class="modal-body">
  23. <button type="button" id="btn" value="Close" class="close" data-dismiss="modal">×</button>
  24. {{msg}}
  25. </div>
  26. <!-- dialog buttons -->
  27. <div class="modal-footer">
  28. <button type="button" ng-click="alertmsg()" class="btn btn-primary">OK</button>
  29. </div>
  30. </div>
  31. </div>
  32. </div>
  33. <div class="container-fluid">
  34. <div class="panel panel-success" style="width: 50%;">
  35. <div class="panel-heading">Login</div>
  36. <div class="panel-body" style="box-shadow: -6px 2px 46px 7px #888888; padding: 20px;">
  37. <form name="loginForm" novalidate>
  38. <div class="form-horizontal">
  39. <div class="form-group">
  40. <div class="row">
  41. <div class="col-md-3" style="text-align: right;">
  42. Username :
  43. </div>
  44. <div class="col-md-6">
  45. <div class="input-group">
  46. <input type="text" class="form-control" id="Uname" placeholder="Username" ng-model="uName" name="Username" required autofocus />
  47. <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. <div class="form-group">
  53. <div class="row">
  54. <div class="col-md-3" style="text-align: right;">
  55. Password :
  56. </div>
  57. <div class="col-md-6">
  58. <div class="input-group">
  59. <input type="password" class="form-control" id="password" placeholder="Password" ng-model="password" name="Password" required autofocus />
  60. <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
  61. </div>
  62. </div>
  63. <div class="form-group">
  64. <div class="row">
  65. <div class="col-md-6">
  66. <div id="divLoading" style="margin: 0px; padding: 0px; position: fixed; right: 0px; top: 0px; width: 100%; height: 100%; background-color: #666666; z-index: 30001; opacity: .8; filter: alpha(opacity=70); display: none">
  67. <p style="position: absolute; top: 30%; left: 45%; color: White;">
  68. please wait...<img src="~/Content/images/load.png">
  69. </p>
  70. </div>
  71. </div>
  72. </div>
  73. </div>
  74. <div class="form-group">
  75. <div class="row">
  76. <div class="col-md-5" style="text-align: right;">
  77. <button id="btnLogin" type="submit" class="btn btn-success" ng-disabled="!(password && uName)" ng-click="LoginCheck()">Login</button>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. </form>
  83. </div>
  84. </div>
  85. </div>
  86. </div>
  87. </body>
  88. </html>
Apply CSS and Scripts file
  • Find BundleConfig.cs in App_Start folder.
  • Here we register the css snd js files to use with the layout page.
  1. public static class BundleConfig
  2. {
  3. public static void RegisterBundles(BundleCollection bundles)
  4. {
  5. RegisterStyleBundles(bundles);
  6. RegisterJavascriptBundles(bundles);
  7. }
  8. private static void RegisterStyleBundles(BundleCollection bundles)
  9. {
  10. bundles.Add(new StyleBundle("~/css")
  11. .Include("~/Content/bootstrap.css")
  12. .Include("~/Content/carousel.css")
  13. .Include("~/Content/site.css"));
  14. }
  15. private static void RegisterJavascriptBundles(BundleCollection bundles)
  16. {
  17. bundles.Add(new ScriptBundle("~/js")
  18. .Include("~/Scripts/jquery-{version}.js")
  19. .Include("~/Scripts/jquery-ui-{version}.js")
  20. .Include("~/Scripts/bootstrap.js"));
  21. }
  22. }

Start by creating your own app.

Read more articles on AngularJS: