0

I am trying to pass an object to my API but it keeps showing the object as NULL in the API.

My Ajax call:

$.ajax({
   type: "GET",
   url: url,
   data: tranData,
   dataType: "json",
   success: function(response){
   var itemcount = response.itemsCount;
   var dataReturned = response.data;
}});

My API Method:

[HttpGet]
        [Route("Api/ReportApi/SummaryReport")]
        public IHttpActionResult SummaryReport(JObject tranData)
        {     

            dynamic oData = tranData;
            BankTransactionsViewModelResults transactions = new BankTransactionsViewModelResults();

            transactions = oData;

            var summarisedTransactions = SummariseResults(transactions.BankTransactionsViewModelList, "Test Ref");

            var response = new
            {
                data = summarisedTransactions,
                itemsCount = summarisedTransactions.Count(),
            };

            return Request.GetOKRequest(response);
        }

The object I am trying to pass:

[{"id":16,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10a","description":"TEST10d","amount":600000.0,"balance":600000.0,"tranDate":1494374400000},

{"id":17,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"WITHDRAWAL","reference":"TEST10b","description":"TEST10d","amount":-400000.0,"balance":200000.0,"tranDate":1494460800000},

{"id":18,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10c","description":"TEST10c","amount":700000.0,"balance":900000.0,"tranDate":1494547200000},

{"id":19,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"TRANSFER","reference":"TEST10d","description":"TEST10d","amount":-600000.0,"balance":300000.0,"tranDate":1494633600000},

{"id":20,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10e","description":"TEST10e","amount":800000.0,"balance":1100000.0,"tranDate":1494720000000},

{"id":21,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"WITHDRAWAL","reference":"TEST10f","description":"TEST10f","amount":-800000.0,"balance":300000.0,"tranDate":1494806400000},

{"id":22,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10g","description":"TEST10g","amount":900000.0,"balance":1.2E7,"tranDate":1494892800000},

{"id":23,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"TRANSFER","reference":"TEST10h","description":"TEST10h","amount":-400000.0,"balance":800000.0,"tranDate":1494979200000},

{"id":24,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10i","description":"TEST10i","amount":800000.0,"balance":1.6E7,"tranDate":1495065600000},

{"id":25,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"TRANSFER","reference":"TEST10j","description":"TEST10j","amount":-800000.0,"balance":800000.0,"tranDate":1495152000000},

{"id":26,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"DEPOSIT","reference":"TEST10k","description":"TEST10k","amount":900000.0,"balance":1.7E7,"tranDate":1495238400000},

{"id":27,"accountId":11111000010,"clientId":1,"agentId":1,"tranType":"WITHDRAWAL","reference":"TEST10l","description":"TEST10l","amount":-700000.1,"balance":0.0,"tranDate":1495324800000}]

How can I pass this to my API? tranData is appearing as null but before being sent I can see the data is there...? I get this data from an external API call and get it from the response:

var tranData = JSON.parse(req.response);
Rajan Mishra
  • 1,178
  • 2
  • 14
  • 30
6dev6il6
  • 767
  • 2
  • 15
  • 34
  • 2
    See this [How to pass json POST data to Web API method as object](https://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-object/20226220#20226220) – Shyju Aug 31 '17 at 12:51
  • Possible duplicate of [How to pass json POST data to Web API method as object](https://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-object) – Munzer Aug 31 '17 at 12:57

2 Answers2

1

In case you want to use HttpGet to complete the request, you will have to use strictly typed object like mentioned in this StackOverflow question. And you may change your method signature to the following

[HttpGet]
[Route("Api/ReportApi/SummaryReport")]
public IHttpActionResult SummaryReport([FromUri]YourCustomClass tranData)
{
    // your code
}

A better alternative will be to use HttpPost. In that case you will have to use JToken not JObject. Here is how your signature will look like

[HttpPost]
[Route("Api/ReportApi/SummaryReport")]
public IHttpActionResult SummaryReport([FromBody]JToken tranData)
{
    BankTransactionsViewModelResults transactions = tranData.ToObject<BankTransactionsViewModelResults>();

    // other processing code
    ....
}
Gaurav Chouhan
  • 265
  • 2
  • 13
0

First create a class with id,accountId,clientId,agentId and so on what you are passing

Next create Object of class

[HttpGet]
    [Route("Api/ReportApi/SummaryReport")]
    public IHttpActionResult SummaryReport(Class classobj)
    { your code here  }

after in ajax you need send json data Format

like tranData{{"id":16,"accountId":11111000010,"clientId":1 like so on}

Thirupathi cst
  • 127
  • 2
  • 10