I have a basic HTML which is calling a WebAPI function using jquery ajax call. HTML sends an array of objects which should get mapped to the functions parameter that i am receiving as LIST. If i remove array and send only 1 object and also remove list from function, then my code works and object is passed successfully to the parameter.
JavaScript code is as below
function Call_Service () {
var input =
{
STATUS: "MY New Status",
CATEGORY: "My Value"
};
var input2 =
{
STATUS: "MY New Status2",
CATEGORY: "My Value2"
};
var input_array = new Array();
input_array[0] = input;
input_array[1] = input2;
$.ajax({
type: "POST",
url: "http://localhost:34989/api/TMSPortal/objectPOC",
data: input_array,
success: function (response) {
alert(response);
}
});
}
C# WebAPI is as below
public Int64 objectPOC(List<TMS_STATUS> _Status)
{
Int64 retValu = 0;
for (int i = 0; i < _Status.Count; i++)
{
retValu++;
}
return retValu;
}
As i came to know that Web-API currently don't allow more than 1 complex parameter therefore i had make my work done by following workaround. Let me know if someone has better solution:
Changed the Web-API function to receive JObject and then extracted my Complex objects from it. Web-API functions looks as below:
Ajax Call is as follows:
To send multiple complex type parameters, create a model and expose them as properties inside the model. In Web API you can have only 1 complex type input parameter out of the box.