This is my jQuery / JS code:
var selectedMonths = [];
var selectedLocations = [];
var selectedEvents = [];
$("#filter").click(function ()
{
$('#months li').each(function ()
{
if ($(this).find('input[type=checkbox]').is(':checked'))
{
selectedMonths.push($(this).find('a').attr('data-value'));
}
});
$('#locations li').each(function () {
if ($(this).find('input[type=checkbox]').is(':checked')) {
selectedLocations.push($(this).find('a').attr('data-value'));
}
});
$('#events li').each(function () {
if ($(this).find('input[type=checkbox]').is(':checked')) {
selectedEvents.push($(this).find('a').attr('data-value'));
}
});
var months = { months: selectedMonths };
$.ajax({
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
url: '/Shop/FilterRinglet',
data: months,
traditional: true,
success: function (result) {
alert("success");
},
failure: function (response) {
alert(response);
}
});
});
And this is my simple controller:
[HttpPost]
public ActionResult FilterRinglet(List<string> months)
{
return Json(new { x = "sdg", received = "ok" });
}
But Chrome DevTools only show an 500 - Internal Server Error.
I want to send three string arrays to that controller (in example you see only the attempts of sending one).
I tried this ( How can I post an array of string to ASP.NET MVC Controller without a form? ) and may other answers. I can´t get this to work, would you please tell me where my mistake is hidden?
Edit: I also tried the following line of code
var months = JSON.stringify({ months: selectedMonths });
selectedMonths looks like this
That does not lead to a 500, but in controller it looks like this:
Edit2: I tried this, too:
$.post('/Shop/FilterRinglet', { 'months[]': selectedMonths }, function (response) {
alert(response);
});
but nothing except ""
reaches the controller.
Edit3:
I changed
[HttpPost]
public ActionResult FilterRinglet(List<string> months)
to
[HttpPost]
public ActionResult FilterRinglet(string[] months)
but still no success.
Edit4:
I also created a class
public class RingletData
{
public List<string> months { get; set; }
}
and changed the method accordingly. months is still ""
.