Description:
In this example
we explain that how to check Username is Exists in Database using AngularJS in
Asp.Net MVC.or how to check Username in available in Current Database table
using AngularJS in MVC Razor. Or how to check user is existing or not using
AngularJS in MVC.or how to check Username availability in database or not using
AngularJS in MVC.
public class EmployeeController
: Controller
{
public ActionResult Index()
{
return
View();
}
[HttpPost]
public
JsonResult CheckUsername(string username)
{
UsersEntities entities = new UsersEntities();
bool
isValid = !entities.Users.ToList().Exists(p => p.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase));
return
Json(isValid);
}
}
View:
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<title>Check
Username Availability (Exists) in Database using AngularJS in ASP.Net MVC</title>
</head>
<body>
<div ng-app="MyApp" ng-controller="MyController">
Email:
<input type="text" ng-model="Username" ng-keyup="Clear()"/>
<input type="button" value="Show Availability" ng-click="CheckAvailability()"/>
<br/>
<span ng-bind="Message" ng-style="{color:Color}"></span>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
var app
= angular.module('MyApp', [])
app.controller('MyController',
function ($scope, $http) {
$scope.CheckAvailability = function () {
$http({
method: "POST",
url: "/Employee/CheckUsername",
dataType: 'json',
data: '{username: "' + $scope.Username + '" }',
headers: { "Content-Type": "application/json"
}
}).success(function (data, status) {
if
(data) {
//Email available.
$scope.Color = "green";
$scope.Message = "Email is Exists";
}
else
{
//Email not available.
$scope.Color = "red";
$scope.Message = "Email is NOT Exists";
}
});
};
$scope.Clear = function () {
$scope.Message = "";
};
});
</script>
</body>
</html>
0 comments:
Post a Comment