2009-04-13 9 views
2

検索ボタンを使用してページ上部が検索基準になっている検索ページがあります。画面の下部は、検索ボタンが押されたときの結果です。この場合、私はユーザーが入力できる6種類の検索基準を持っています。すべての条件を1つのクラスにまとめて、ControllerアクションでJsonオブジェクトをクラスとして読み取ることができます。 FireBugを使用すると、私のJsonが正しく構築されているのがわかります。デバッガを使用すると、コントローラ/アクションが起動していることがわかります。しかし、コントローラ/アクションでデバッガを使ってクラスオブジェクトを見ると、すべてのプロパティがnullまたはゼロになります。Jsonを使用してJsonを使用してコントローラ/アクションを呼び出す

ここは私のコントローラです。 [AcceptVerbs(HttpVerbs.Post)] 公共のActionResult GetStudentByCritera(StudentSearchCriteraCVのcritera) {//データ ViewDataを[ "MainData"] = studentBLLHdl.StudentFind(critera)を取得します。 戻るView(); ここ}

機能LoadResultTable(...コントローラを呼び出すことのJavaScript/jQueryのです) { //が するvar dataToSend = BuildJsonを(送信するためにJSONオブジェクトを構築)

 $.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ポストである、したがって、データPARAMTERのみがキーと値のペアではなく、JSONオブジェクトをすることができ。 dataForClassを平坦化するとうまくいきます。

関連する問題