user1175857
user1175857

Reputation: 115

Can not get the ASP MVC4 APIController Post Method to read JSON

I'm having problems posting my JSON data back to my api controller in my MVC 4 project. Here's my javascript:

 self.saveInvestment = function () {
        var investmentdata = ko.mapping.toJSON( { "InvestmentSummary": self.InvestmentSummary } );

        $.ajax({
            url: "../api/investment/",
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: investmentdata,
            success: function () {
                alert('hi');
            }
        });

    };

Here's my controller code:

[HttpPost]
        public void Post([FromBody]InvestmentSummary val)
        {
            string blah;
            if (val.InvestmentDetails[0] != null)
                blah = val.InvestmentDetails[0].Cost.ToString();

        }

Here's the header info from fiddler:

application/json; charset=UTF-8
http://localhost:1961
http://localhost:1961/Investment
X-Requested-With: XMLHttpRequest

POST http://localhost:1961/api/investment/ HTTP/1.1
Host: localhost:1961
Connection: keep-alive
Content-Length: 2470
Origin: http://localhost:1961
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Content-Type: application/json; charset=UTF-8
Accept: */*
Referer: http://localhost:1961/Investment
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{"InvestmentSummary":{"InvestmentDetails":[{"Cost":"11","FairValue":0,"Tranches":[{"IDTranche":null.......

The JSON is formatted properly and I double checked with JSON Lint. When I debug my application, in my controller, "val" has no data in it. What am I doing wrong?? Thanks!

Edit: Here's my class definition of InvestmentSummary:

    public class InvestmentSummary : TransactionSummary
    {
        public InvestmentSummary()
        {
            this.InvestmentDetails = new List<InvestmentDetail>();
        }

        public List<InvestmentDetail> InvestmentDetails { get; set; }
    }

I think my problem is that my JSON is using InvestmentDetails but my class name is InvestmentDetail? Anyone, please help? Thanks

Second Edit* My full JSON has this structure:

var init = {
        InvestmentSummary: {
            InvestmentDetails: [],
            IDTransactionSummary:null,
            TxnParameter: {},
            TxnDate: "",
            Fund: {},
            PortfolioCompany: {}
        }
    };

From the help of tpeczek, I was able to read in InvestmentDetails. But now how do I also get the rest of my JSON? In my javascript, the JSON is built properly however, when it reaches my controller, only the InvestmentDetails are present. All other children of InvestmentSummary is gone! Thanks

Upvotes: 1

Views: 920

Answers (1)

tpeczek
tpeczek

Reputation: 24125

First you shouldn't be wrapping the object into one more object in your JavaScript:

 self.saveInvestment = function () {
        var investmentdata = ko.mapping.toJSON(self.InvestmentSummary);

        $.ajax({
            url: "../api/investment/",
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: investmentdata,
            success: function () {
                alert('hi');
            }
        });

    };

Also you shouldn't need FromBody in this case:

[HttpPost]
public void Post(InvestmentSummary val)
{
    string blah;
    if (val.InvestmentDetails[0] != null)
    blah = val.InvestmentDetails[0].Cost.ToString();    
}

Upvotes: 1

Related Questions