2009-04-13 48 views
2

我有一個搜索頁面,其中頁面的頂部是搜索條件與搜索按鈕。屏幕底部是按下搜索按鈕時的結果。在這種情況下,我有6種不同的搜索條件供用戶輸入。我想將所有的標準綁定到一個類中,所以我的Controller操作可以將Json對象作爲類讀取。使用FireBug我能夠看到我的Json構建正確。使用調試器我知道我的控制器/操作正在被解僱。但是,當我使用Controller/Action中的調試器查看類對象時,所有屬性都爲null或零。JQuery使用Json調用控制器/動作

這是我的控制器...。 的[AcceptVerbs(HttpVerbs.Post)] 公共的ActionResult GetStudentByCritera(StudentSearchCriteraCV性判據) { //獲取數據 計算機[ 「MainData」] = studentBLLHdl.StudentFind(性判據); return View(); }

這裏是JavaScript/JQuery的調用控制器... 功能LoadResultTable(){ // 構建JSON對象發送 VAR dataToSend = BuildJson()

 $.ajax({ 
      url: "GetStudentByCritera", 
      type: 'POST', 
      data: dataToSend, 
      dataType: 'json', 
      contentType: "application/json; charset=utf-8", 
      beforeSend: ClientSideValidate, 
      success: function(result) 
      { 
       alert(result.Result); 
       $('#SearchResult').html(result.Result).show(); 
       // UnBlock UI 
      }, 
      error: function (XMLHttpRequest, textStatus, errorThrown) 
      { 
       // UnBlock UI 

       // not sure how to handel error 
       alert("error happen when posting to 'GetStudentByCritera'") 

       // typically only one of textStatus or errorThrown 
       // will have info 
       this; // the options for this ajax request 
      } 


     }); 
    } 

    function BuildJson() 
    { 
     // building Json    

     var dataForClass = { "StudentSearchCriteraCV" : [{ 
      "StudLname": $("input[name='StudentSearchCriteraCV.StudLname']").val(), 
      "StudFname": $("input[name='StudentSearchCriteraCV.StudFname']").val(), 
      "Ssn": $("input[name='StudentSearchCriteraCV.Ssn']").val(), 
      "StudId": $("input[name='StudentSearchCriteraCV.StudId']").val(), 
      "Sex": $("input[name='StudentSearchCriteraCV.Sex']").val(), 
      "Race": $("input[name='StudentSearchCriteraCV.Race']").val() 
      }]}; 

     return $.toJSON(dataForClass); 
    } 

    function ClientSideValidate() 
    { 
     // Block the UI 
     alert("In the ClientSideValidate"); 

     // if error UNBlock UI 
     // return true if client side data is good. 

     return true; 
    } 


</script> 

回答

0

我相信你的JSON值需要用引號括起來,除非它是一個數字。

"StudFname": '"' + $("input[name='StudentSearchCriteraCV.StudFname']").val() + '"', 

我還沒有使用.toJSON(),但我相信它會將您的JSON對象變成一個字符串。既然你創建你的對象只是爲了立刻把它變成一個字符串,爲什麼不把它創建爲一個字符串開始呢?這就是我所做的,當我遇到了這個問題(調試器顯示的值爲零。)

var dataForClass = '{ "StudentSearchCriteraCV" : [{ ' + 
     '"StudLname": "' + $("input[name='StudentSearchCriteraCV.StudLname']").val() + '", ' + 
     '"StudFname": "' + $("input[name='StudentSearchCriteraCV.StudFname']").val() + '", ' + 
     '"Ssn": "' + $("input[name='StudentSearchCriteraCV.Ssn']").val() + '", ' + 
     '"StudId": ' + $("input[name='StudentSearchCriteraCV.StudId']").val() + ', ' + //assuming StudID is a number 
     '"Sex": "' + $("input[name='StudentSearchCriteraCV.Sex']").val() + '", ' + 
     '"Race": "' + $("input[name='StudentSearchCriteraCV.Race']").val() +'" ' + 
     '}]}'; 
3

你的Ajax調用僅僅是一個異步HTTP發佈,因此數據放慢參數只能是鍵值對,而不是一個JSON對象。如果你flatten dataForClass它將工作。