天天看点

验证一个值是否已经存在数据表中

仅是在程序中指定某些来值来匹配。

为你的数据表准备一个存储过程:

验证一个值是否已经存在数据表中

判断是否已经存在此值。只需写SELECT语句。如果没有记录返回,它将返回null。

有ASP.NET MVC程序中,创建model:

验证一个值是否已经存在数据表中
还要创建一个实体方法,是程序与数据协作与沟通的:
验证一个值是否已经存在数据表中
程序逻辑部分,还要写一个接口,供前端angularjs去读取:
验证一个值是否已经存在数据表中

返回给前端exists布尔值。

 Ok,进行到前端angularjs时,写自定义指令Directive:

验证一个值是否已经存在数据表中
验证一个值是否已经存在数据表中
验证一个值是否已经存在数据表中

airExpressApp.directive('validateCountry', function ($http, $q) {
    return {
        restrict: 'AE',
        require: 'ngModel',
        link: function ($scope, element, attributes, ngModelController) {
            ngModelController.$asyncValidators.countryExists = function (modelValue, viewValue) {
                var deferred = $q.defer();

                var obj = {};
                obj.Country_EN = modelValue;
                $http({
                    method: 'POST',
                    url: '/Code/CountryByKey',
                    dataType: 'json',
                    headers: {
                        'Content-Type': 'application/json; charset=utf-8'
                    },
                    data: JSON.stringify(obj),
                }).then(function (response) {
                    if (response.data.exists) {
                        deferred.resolve();
                    } else {
                        deferred.reject();
                    }
                });
                return deferred.promise;
            };
        }
    }
});      

Source Code

文本框设置自定义指令属性:

验证一个值是否已经存在数据表中

继续阅读