Reputation: 361
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;
}
Upvotes: 1
Views: 7012
Reputation: 361
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:
public Int64 objectPOC(JObject jsonWrapper)
{
dynamic jsonValues = jsonWrapper;
JArray jsonInput = jsonValues.input;
JArray jsonInput2 = jsonValues.input2;
List<TMS_STATUS> _Status = jsonInput.ToObject<List<TMS_STATUS>>();
List<TMS_STATUS> _Status2 = jsonInput2.ToObject<List<TMS_STATUS>>();
Int64 retValu = 0;
for (int i = 0; i < _Status.Count; i++)
{
retValu++;
}
return retValu;
}
Ajax Call is as follows:
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;
alert(input_array[0].STATUS);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost:34989/api/TMSPortal/objectPOC",
dataType: "json",
data: JSON.stringify({
input: input_array,
input2: input_array
}),
success: function (response) {
alert(response);
}
});
}
Upvotes: 3
Reputation: 2965
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.
Upvotes: 0