Click here to Skip to main content
15,905,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a web page which posts data to an Web API.
The ajax function is as follows.

JavaScript
var jsondata = { "comparisionCalculatorReq": { "transactionType": "None", "fundName": "Myfund", "annualizedReturn": "10", "frequency": "Monthly"} };  
  
$.ajax({  
type: "POST",  
url: "http://localhost:1101/Mypost/PostDataAPI",  
dataType: 'json',  
data: jsondata  
}).done(function (msg) {  
alert(msg);  
});  


In my Web API controller i have a method as below

C#
[ActionName("PostDataAPI")]  
       public string PostDataAPI(JSONdata obj)  
       {  
           try  
           {  
               string k = obj.transactionType;  
               return k;  
           }  
           catch (Exception err)  
           {  
               throw err;  
           }  
       }  



The JSONdata class is below

C#
public class JSONdata  
   {  
       public string transactionType { get; set; }  
       public string fundName { get; set; }  
       public string annualizedReturn { get; set; }  
       public string frequency { get; set; }  
   }  



Problem is that, I'm getting null value for transactionType in my controller.

If the json data is passed like this in ajax,

JavaScript
var jsondata={ "transactionType": "None", "fundName": "Myfund", "annualizedReturn": "10", "frequency": "Monthly"};


then all the values are got correct.
Please provide a solution for this.

What I have tried:

I have tried making the JSONdata class as below, but still it is returning null for every values

C#
public class JSONdata  
   {  
       public object comparisionCalculatorReq { get; set; }
       public string transactionType { get; set; }  
       public string fundName { get; set; }  
       public string annualizedReturn { get; set; }  
       public string frequency { get; set; }  
   }  
Posted
Updated 11-Aug-16 22:35pm

I modified your ajax code:

JavaScript
var jsondata = { "comparisionCalculatorReq": { "transactionType": "None", "fundName": "Myfund", "annualizedReturn": "10", "frequency": "Monthly"} };  
  
$.ajax({  
type: "POST",  
url: "http://localhost:1101/Mypost/PostDataAPI",  
contentType:"application/json; charset=utf-8",
dataType: 'json',  
data: JSON.stringify(jsondata)  
}).done(function (msg) {  
alert(msg);  
}); 
 
Share this answer
 
Comments
Midhun T P 12-Aug-16 2:05am    
adding content type will cause cross origin error. So can't add that. Also i want solution without stringifying the data
Dil0500 12-Aug-16 2:18am    
Look into this link may be it will help you
http://stackoverflow.com/questions/6114436/access-control-allow-origin-error-sending-a-jquery-post-to-google-apis
I got the solution. Changed in model class like below

C#
public class JSONdata
   {
       public comparisionCalculatorReq[] comparisionCalculatorReq { get; set; }
   }
   public class comparisionCalculatorReq
   {
       public string transactionType { get; set; }
       public string fundName { get; set; }
       public string annualizedReturn { get; set; }
       public string frequency { get; set; }
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900