Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...