I want to send an array of objects from an ajax call to a controller action.
On the server side, I have
container classes:
public class MyContainer
{
public string Id { get; set; }
public Filter[] Filters { get; set; }
}
public class Filter
{
public string Name { get; set; }
public string[] Values { get; set; }
}
and action:
public ActionResult MyAction(MyContainer container)
{
var id = container.Id;
foreach(Filter filter in container.Filters)
{
}
}
On the interface side, I have
$(document).on('click', 'mySelector', function (event) {
var firstIds = {};
firstIds.Name = "Custom Name 1";
firstIds.Values = GetIds('param1');
var secondIds = {};
secondIds.Name = "Custome Name 2";
secondIds.Values = GetIds('param2');
var Id = $(this).attr('id');
var filters = [];
filters.push(firstIds);
filters.push(secondIds);
$.ajax({
method: "GET",
url: baseUrl+"/MyAction",
data:
{
Id: Id,
Filters: filters
},
contentType: 'application/json',
success: function (res) {
alert('success')
}
});
});
So, if I use it, as in the example above, the container object in action has an Id value and has an array of 2 elements in Filters, however both of them have a name and values as null.
With the traditional set True, I got container.Id installed, but container.Filters = null.
Any suggestion? Thank.
source
share