I am having some trouble with $.post()
and my mvc controller.
I have tried a couple of examples on the web and stack overflow like this : How can I post an array of string to ASP.NET MVC Controller without a form?
but without success. I simply want to pass an array of string to my controller :
[HttpPost]
public HttpResponseMessage Post([FromBody]List<string> idList, string request, bool auto)
{
LogHelper.Log(Severity.Info, "Just entered in POST api/files");
var fileList = new List<string>();
switch (request)
{
case "Activate":
fileList = auto ? _bendingsServies.GetBendigsToCome() : idList;
_filesServices.Activate(fileList);
break;
case "Archive":
fileList = auto ? _filesServices.GetFilesToArchive() : idList;
_filesServices.Archive(fileList);
break;
default:
return ControllerContext.Request.CreateResponse(HttpStatusCode.BadRequest);
break;
}
return ControllerContext.Request.CreateResponse(HttpStatusCode.OK);
}
and my jquery :
@using System.Configuration
var api_url = "@ConfigurationManager.AppSettings["apiUrl"]";
$(document).ready(function() {
// ---- Ajax calls ---- //
$("#archivebutton").click(function() {
var url = api_url + "Files?request=Archive&auto=false";
var fileList = ["10", "14", "12"];
var postData = { idList: fileList }
$.post(url, $.param(postData,true));
});
});
With everything I tried, the idList
always contain 0 elements when I try it.
// ---- EDIT ---- //
I am able to send the data if I encapsulate it in a json object :
$("#activatebutton").click(function() {
var url = api_url + "Files?request=Activate&auto=false";
var fileList = ["this", "is", "a", "test"];
$.post(url, { fileList: fileList});
});
and this is the request :