在我的web api控制器中有一个如下所示的操作方法
[HttpPost]
[EnableCors(origins: "*", headers: "*", methods: "*", exposedHeaders: "X-Custom-Header")]
public IReportOutput InsuranceHandlingFiles([FromBody]CaseCountsInputData caseCountsInputData)
{
}
此操作的参数类是
[Serializable]
public class CaseCountsInputData
{
public string MainTitle { get; set; }
public string YearTitle { get; set; }
public string InsurerFileCountTitle { get; set; }
public List<FileCount> FileCounts { get; set; }
public int InsurerTotalCount { get; set; }
public int GrandTotal { get; set; }
public string TotalTitle { get; set; }
public string GrandTotalTitle { get; set; }
}
[Serializable]
public class FileCount
{
public string year { get; set; }
public int InsurerFilesCount { get; set; }
}
我将此API方法称为测试目的,如下所示。我的动作以这种方式调用,我的json对象绑定到CaseCountsInputData类,但所有参数都为null。你能帮我确切地告诉我错误吗?
$("#btnExport")
.click(function() {
var reportData = GetReportData();
console.log(JSON.stringify(reportData));
$.ajax({
type: "POST",
dataType: "json",
data: JSON.stringify(reportData),
contentType: "application/json",
url: "http://localhost:50773/api/export/insurancehandlingfiles",
success: function(data) {
var DocumentBody = data.Data;
var FileName = data.FileName;
dataURItoBlob(DocumentBody, FileName);
},
error: function(error,as,asd) {
jsonValue = jQuery.parseJSON(error.responseText);
alert("error" + error.responseText);
}
});
});
});
function GetReportData() {
var reportModel = {
CaseCountsInputData: {
MainTitle: "Dosya Sayısı",
YearTitle: "Yıl",
InsurerFileCountTitle: "Sigortacı Dosya Sayısı",
TotalTitle: "Toplam",
GrandTotalTitle: "Genel Toplam",
InsurerTotalCount: 1,
GrandTotal: 3,
FileCounts: []
}
}
var caseCounts =[];
caseCounts.push({
"year": 2014,
"InsurerFilesCount": 1
});
caseCounts.push({
"year": 2015,
"InsurerFilesCount": 4
});
for (var i = 0; i < caseCounts.length; i++) {
reportModel.CaseCountsInputData.FileCounts.push(caseCounts[i]);
}
return reportModel;
}