アプリケーションでレポートを表示しています。ユーザーには、レポートをフィルタリングする機能があります。フィルタ データを入力し、[Go] ボタンをクリックします。[Go] をクリックすると、jQuery を使用してフォーム全体をコントローラー アクションに渡します。フォーム コレクションのコントロールの値に基づいてデータベースからデータを読み取り、データをフォームに戻し、レポートを新しいデータでリロードする必要があります。
次のコードを試しました。
.cshtml
<script type="text/javascript">
$(document).ready(function () {
$('#action_button').click(function () {
//$('#trialReportForm').attr("action", '@Url.Action("ReportFilter", "MyController")');
//$('#trialReportForm').submit();
// While using above two commented lines instead of the below codes, I get the form collection correctly. BUt I cannot pass back the new data to the form. If this is the solution.. How can I pass the data back to form to reload the report?
var formElements = $("#trialReportForm").serialize();
//var data = { "parameter": $.toJSON(formElements) };
var data = { "parameter": formElements };
$.ajax({
url: @Url.Action(ReportFilter", "MyController"),
type: 'POST',
data: data,
dataType: 'json',
success: OnSuccess,
error: OnFailure
});
});
function OnSuccess(result)
{
alert(result);
}
function OnFailure(result)
{
alert(result);
}
});
controller.cs
[HttpPost]
public JsonResult ReportFilter(string parameter)
{
return new DBConnect().GetFilterData(parameter);
}
以下のコードも試しました。しかし、パラメータは空です
[HttpPost]
public JsonResult ReportFilter(FormCollection parameter)
{
return new DBConnect().GetFilterData(parameter);
}
コールインアクションメソッドを取得しています。ただし、ここでは、パラメーターはシリアル化された形式になっています。しかし、それをデシリアライズすることはできません。他のフォームのコレクションを形成するためにデシリアライズするにはどうすればよいですか? 私が欲しいのは、フォーム内の入力コントロールの値を取得することだけです。
デシリアライズのために以下の2つのコードを試しました。しかし、どれもうまく機能していません。例外を取得するだけです。
1: var serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<FormCollection>(parameter);
2: var request = JsonConvert.DeserializeObject<FormCollection>(parameter);