Click here to Skip to main content
14,269,736 members
Rate this:
Please Sign up or sign in to vote.
Hi,
I have a web page which posts data to an Web API.
The ajax function is as follows.

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

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



The JSONdata class is below

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,

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

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
Rate this:
Please Sign up or sign in to vote.

Solution 1

I modified your ajax code:

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);  
}); 
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

I got the solution. Changed in model class like below

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; }
   }
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100