I want to POST an empty javascript array []
to webAPI and have it create an empty list of integers. I also want it so if I post javascript null
to webAPI that it assigns null to the list of integers.
JS:
var intArray = [];
$.ajax({
type: 'POST',
url: '/api/ListOfInts',
data: {'' : intArray},
dataType: 'json'
});
c# webapi
[HttpPost]
public void ListOfInts([FromBody]List<int> input)
Problem 1) Jquery refuses to send data {'' : []}
as the post payload. As soon as I add something in the array it works such as {'' : [1,2,3]}
Problem 2) Passing empty js array to controller gives null Based on what i read even if I do get it to post an empty array, it will initialize the list as null. Discussed solutions have it so that the list is always initializes as empty but I don't want that. I want it to be null in some case (when null/undefined is sent to it) and when []
is sent it should initialize as empty.
Edit: See https://www.asp.net/web-api/overview/advanced/sending-html-form-data-part-1 about why I am using {'' : []}