Hi mahesh213,
Refer below code.
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult CheckExistance(int refNo)
{
List<Reference> references = new List<Reference>();
references.Add(new Reference { Id = 1, Name = "mahesh", RefNo = 123 });
bool isExist = references.Where(x => x.RefNo == refNo).Count() > 0 ? true : false;
return Json(isExist, JsonRequestBehavior.AllowGet);
}
public class Reference
{
public int Id { get; set; }
public string Name { get; set; }
public int RefNo { get; set; }
}
}
View
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.8/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $http) {
$scope.IsValid = true;
$scope.IsExist = false;
$scope.Validate = function () {
$http({
method: 'POST',
url: '/Home/CheckExistance/',
params: { refNo: $scope.RefNo }
}).then(function (response) {
if (response.data) {
$scope.IsValid = false;
$scope.IsExist = true;
} else {
$scope.IsValid = true;
$scope.IsExist = false;
}
});
}
$scope.Save = function () {
if (!$scope.IsExist) {
// Do your Task.
// Set the variable to false.
$scope.IsExist = false;
}
}
})
</script>
</head>
<body>
<div ng-app="MyApp" ng-controller="MyController">
Name:<input type="text" ng-model="RefNo" ng-change="Validate()" />
<span style="color: Red" ng-show="!IsValid">RefNo already exists!</span><br />
<button type="button" ng-click="Save()">Save</button>
</div>
</body>
</html>