3
Answers

Issue with Posted JSON data to Web API

Midhun T P

Midhun T P

8y
300
1
Hi,
I have a web page which posts data to an Web API.
The ajax function is as follows.
  1. var jsondata = { "comparisionCalculatorReq": { "transactionType""None""fundName""Myfund""annualizedReturn""10""frequency""Monthly"} };  
  2.   
  3. $.ajax({  
  4. type: "POST",  
  5. url: "http://localhost:1101/Mypost/PostDataAPI",  
  6. dataType: 'json',  
  7. data: jsondata  
  8. }).done(function (msg) {  
  9. alert(msg);  
  10. });  
 In my Web API controller i have a method as below
 
  1. [ActionName("PostDataAPI")]  
  2.        public string PostDataAPI(JSONdata obj)  
  3.        {  
  4.            try  
  5.            {  
  6.                string k = obj.transactionType;  
  7.                return k;  
  8.            }  
  9.            catch (Exception err)  
  10.            {  
  11.                throw err;  
  12.            }  
  13.        }  
The JSONdata class is below
  1. public class JSONdata  
  2.    {  
  3.        public string transactionType { getset; }  
  4.        public string fundName { getset; }  
  5.        public string annualizedReturn { getset; }  
  6.        public string frequency { getset; }  
  7.    }  
Problem is that, I'm getting null value for transactionType in my controller.
 
If the json data is passed like this in ajax,
  1. var jsondata={ "transactionType""None""fundName""Myfund""annualizedReturn""10""frequency""Monthly"};  
 then all the values are got correct.
Please provide a solution for this. 
 
 
Answers (3)