検索ボタンを使用してページ上部が検索基準になっている検索ページがあります。画面の下部は、検索ボタンが押されたときの結果です。この場合、私はユーザーが入力できる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>