0

I am working on a form in mvc3 and use form validations in javascript and ajax. in my form i add code and description in database and before form submission want to check that code already exist in database or not.i get the code in javascript through ajax function call andd eturn data in json form. when i get the data i display error message in alert to user that code already exist. but my alert is not display.what can i do for it. below is my javascript save button click function

  $('#sve').click(function () {
    //e.preventDefault();
      var iscodeexis = CodeExistChk();
    if (iscodeexis) {//



        //***********************CODE TO SAVE DATA IN DATABASE***********************************
        var person = { AcCode: $('#AcCode').val(), Descrip: $('#Descrip').val(), AddOn: dd };
        $.ajax({
            url: '/Home/Save?action=Sve',
            type: "POST",
            data: JSON.stringify(person),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                //   $('#message').html('Record saved successfully' + result).fadeIn();
                alert("Record saved successfully");
            },
            error: function () {
                //    $('#message').html('Error Occurred').fadeIn(); 
                alert("Record not saved successfully");
            }
        });
    }//end of is valid function chk
    else
        return false;//if isvalid function return false then save button also return false

});  //end button clcik function


 function CodeExistChk() {
    subA = $('#AcCode').val().trim();
    //  ===========================check whether code exist already or not
    if (subA.length === 10) {
        str1 = "select AcCode from Account where AcCode='";
        str2 = str1 + subA + "'";
        GetCodeData(str2); //check whether code exist or not
        strRes = strRes.substring(1, strRes.length - 1);
        if (strRes.length > 0 && strRes != "") //if  code exist then return false and not allow     to enter code   
        {
            alert('Code already exist cannot insert record');
            return false;
        }

    }
    //===============================

    }

below is getcodedata function which use in above code to get code from database

    //===============FUNCTION TO GET CODE FROM DATABASE TO USE IN JS FILE==========
function GetCodeData(Str) {

    var p = {
        StrSql: Str

    };
    $.ajax({
        url: '/Home/GetGenVal',
        type: 'POST',
        // contentType: 'application/x-www-form-urlencoded',
        dataType: "JSON",
        contentType: "application/json; charset=utf-8",
        processData: false,
        crossDomain: false,
        traditional: true,
        data: JSON.stringify(p),
        cache: false,
        //   success: callback
        success: function (data) {
            //$("#Descrip").val(data);
            //  ResSubCode = data;
            strRes = null;
            strRes = data;
            return strRes;
        }

    });
}

waiting for early solution.

4

1 に答える 1

0

おっと、このコードにはいくつかの間違いがあります。最初は、SQL クエリをクライアント側で作成して実行しないでください。クエリを「削除元」に変更するとどうなりますか? バイバイデータベース!

単純にロジックを編集し、[RemoteAttribute] MVC3/4 機能を使用してコントローラー アクションを呼び出し、単純に true または false を返します。ここで確認してください: Remoteattribute テストの使用法 あなたは間違っているはずがありません!

ヴィンチェンツォ。

于 2013-04-22T09:35:15.210 に答える