Hi mahesh213,
Check this example. Now please take its reference and correct your 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>
<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.Validate = function () {
$http({
method: 'POST',
url: '/Home/CheckExistance/',
params: { refNo: $scope.RefNo }
}).then(function (response) {
if (response.data) {
$scope.IsValid = false;
} else {
$scope.IsValid = true;
}
});
}
})
</script>
</head>
<body>
<div ng-app="MyApp" ng-controller="MyController">
Name:<input type="text" ng-model="RefNo" />
<span style="color: Red" ng-show="!IsValid">RefNo already exists!</span>
<br /><button type="button" ng-click="Validate()">Save</button>
</div>
</body>
</html>
Screenshot
