1. 程式人生 > >驗證一個值是否已經存在數據表中

驗證一個值是否已經存在數據表中

con post link utf-8 程序 部分 ssa lar 準備

前幾天Insus.NET有寫過一篇《angularjs自定義指令Directive》http://www.cnblogs.com/insus/p/6908815.html

僅是在程序中指定某些來值來匹配。

為你的數據表準備一個存儲過程:
技術分享

判斷是否已經存在此值。只需寫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


文本框設置自定義指令屬性:
技術分享

實時演示:
技術分享

驗證一個值是否已經存在數據表中