Problem:
I am trying to send an array of strings via jquery post but they dont get parsed correctly, all I get is null in the list.
javascript:
var array = [];
array.push("test")
array.push("test2")
array.push("tes3")
$.post("Admin/FilteredKeys", $.param(JSON.stringify({ Ids: array, OnlyActive: true }, true)));
C# Model:
public class MySearch
{
public bool OnlyActive { get; set; } = true;
public List<string> Ids { get; set; }
}
action in controller:
public async Task<IActionResult> FilteredKeys(MySearch filter)
{
var data = await _service.GetFilteredKeyTypes(filter);
return View();
}
I've googled and found that the traditional property need to be set to true but it remains the same, i tried this snippet as well:
$.ajax({
type: "POST",
url: "Admin/FilteredKeys",
data: postData,
success: function(data){
alert(data.Result);
},
dataType: "json",
traditional: true
});
This is a .net core project do additional I need to change additional paramaters somewhere?
EDIT:
forgott to add that my original attempt was like this:
$.Admin.worker.postJson("Admin/FilteredKeys", JSON.stringify({ Ids: array, OnlyActive: true }), function (data) {
var t = "";
});
postJson: function (url, data, callback) {
$.LoadingOverlay("show");
$.ajax({
url: url,
type: 'POST',
data: data,
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (callback)
callback(data);
$.LoadingOverlay("hide");
},
error: function (event, jqxhr, settings, thrownError) {
//$.helpers.errorHandler($("#fileDialogErrors"), event.responseText);
$.LoadingOverlay("hide");
}
});
}
data: { filter : postData }in ajax since I faced and solved similar problems.$.param(JSON.stringify({ Ids: array, OnlyActive: true }. Have you simply tried posting the object itself as the post data? e.g.{ Ids: array, OnlyActive: true }