The Json I wish to pass into an MVC 5 controller is as follows:
var searchInputs = { "Id": [ "1", "2" ] };
As you can see, It is just an array of strings called Id.
I have created a class which should receive this array.
public class SearchList
{
public SearchList()
{
Id = new List<string>();
}
public List<string> Id { get; set; }
}
The controller is defined as:
[HttpPost]
public FilePathResult DownloadSelectedPDF(SearchList data)
{
...
}
No errors are thrown, but the controller does not get the data, but returns a zero collection.
As per request, I have added the Ajax call.
$.ajax({
url: url,
method: 'POST',
dataType: "json",
data: data,
success: function (result) {
console.log();
if (result.Success) {
console.log("ajax: result successful");
if (result.SuccessMessage !== null)
showSuccess(result.SuccessMessage);
successCallBack(result.Data);
} else {
console.warn("ajax: result unsuccessful");
showError(result.ErrorMessage);
failCallBack(result.ErrorMessage);
}
},
error: function (jqXHR, textStatus, errorThrown) {
if (!supressWaitingPopup)
waitingModal(false);
console.warn("ajax error");
showError(errorThrown);
failCallBack(errorThrown);
}
});
Where the data is the searchInputs varaiable.
UPDATE:
The searchInput can have multiples so I should have defined it as:
var searchInputs = { "Id": [ "1", "2", ... ] };
FURTHER UPDATE:
I have used the ajax params suggested, both together and separately.
Using the same ajax call, if these params are present, then a call using this structure fails:
this.JsonData = {
"__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val(),
"searchMode":
{
"mode": Number(mode.val()),
"pageSize": Number(pagesize.val()) || 5, //Initial Page Size Set Here
"pageNumber": Number(pagenumber.val()) || 1, //Start on page one
"sortField": sortfield.val() || "PODRef",
"sortDirection": sortdirection.val() || "desc"
},
"searchData":
{
"Compare": Number(StdComparison.val()),
"SearchTextFrom": searchText.val(),
"SearchTextTo": searchTextTo.val()
}
This is true if I JSON.stringify the data or not.