Properties of class parameter are always null in Asp.Net Web Api

て烟熏妆下的殇ゞ 提交于 2019-12-25 07:46:57

问题


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;
    }

来源:https://stackoverflow.com/questions/42976554/properties-of-class-parameter-are-always-null-in-asp-net-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!