2009-04-13 7 views
2

페이지 상단이 검색 버튼이있는 검색 기준 인 검색 페이지가 있습니다. 화면 맨 아래는 검색 버튼을 눌렀을 때의 결과입니다. 이 경우 사용자가 입력 할 수있는 6 가지 검색 기준이 있습니다. 컨트롤러 객체가 클래스로 Json 객체를 읽을 수 있도록 모든 조건을 하나의 클래스로 묶고 싶습니다. FireBug를 사용하면 Json이 올바르게 빌드 된 것을 볼 수 있습니다. 디버거를 사용하여 나는 나의 컨트롤러/액션이 해고되고 있음을 안다. 그러나 Controller/Action에서 디버거를 사용하여 클래스 객체를 보면 모든 속성이 null 또는 0입니다.Json을 사용하여 Json을 호출하여 컨트롤러/액션을 호출합니다.

여기 내 컨트롤러입니다 .... [AcceptVerbs (HttpVerbs.Post)] 공개 ActionResult GetStudentByCritera (StudentSearchCriteraCV의 critera) {// 데이터 을 ViewData [ "MainData"] = studentBLLHdl.StudentFind (critera)을 가져 오기; 돌아 가기 View(); } 여기

이 var에 dataToSend을 보내 { //하는 JSON 객체를 구축 ... 기능 LoadResultTable() 컨트롤러를 호출하는 자바 스크립트/JQuery와있다 = 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 객체를 String으로 변환한다고 생각합니다. 객체를 바로 문자열로 변환하기 위해 객체를 생성하기 때문에 처음부터 문자열로 만들지 않겠습니까? 그게 내가이 문제에 실행했을 때 내가 무슨 짓을했는지 (디버거가 값을 0을 보여주는.)

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 객체가 될 수 있습니다 . dataForClass를 평평하게하면 작동 할 것입니다.

관련 문제