我在我的應用程序中顯示一個報告。用戶可以過濾報告。所以他們輸入過濾數據並點擊'Go'按鈕。在'去'點擊我使用jQuery將整個表單傳遞給控制器操作。我想根據表單集合中控件的值讀取數據庫中的數據,並將數據傳回表單並需要使用新數據重新加載報表。表單數據沒有得到行動方法MVC和jquery ajax
我試過下面的代碼。
.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);
}
我正在調用操作方法。但是這裏的參數是以一些序列化的形式出現的。但是不能將其反序列化。我如何反序列化它以形成其他形式的集合? 我想要的就是獲取表單中輸入控件的值。
我嘗試了以下兩個代碼進行反序列化。但他們都沒有工作正常。只有得到例外。
1: var serializer = new JavaScriptSerializer();
var jsonObject = serializer.Deserialize<FormCollection>(parameter);
2: var request = JsonConvert.DeserializeObject<FormCollection>(parameter);