In my web api controller there is an action method like below
[HttpPost]
[EnableCors(origins: "*", headers: "*", methods: "*", exposedHeaders: "X-Custom-Header")]
public IReportOutput InsuranceHandlingFiles([FromBody]CaseCountsInputData caseCountsInputData)
{
}
Parameter class of this action is
[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; }
}
I call this API method for testing purposes as follows. My action is called this way and my json object is binded to CaseCountsInputData class but all parameters are null. Can you help me exactly where I made the mistake?
$("#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;
}
See Question&Answers more detail:os