I have a controller that takes a List<int> and I am calling from AJAX. The controller is hit, but the parameter is always null.
My controller:
public ActionResult MinorAreas(List<int> majorareas)
{
// ...
}
jQuery call:
function onChange(e)
{
var cur = this.value(); // an array of numbers like [2,4,7,9]
$.ajax({
cache: false,
type: "GET",
traditional: true,
url: "@(Url.RouteUrl("GetMinorAreas"))",
data: { "majorareas": cur},
success: function (data) {...},
error: function (xhr, ajaxOptions, thrownError) {... }
});
}
Route definition:
routes.MapLocalizedRoute(
"GetMinorAreas",
"minorAreas",
new { controller="ProSearch", action="MinorAreas", majorareas=new List<int>() },
new[] { "ABC.ZZZ.Controllers" }
);
Using fiddler, I can see that the URI is built correctly:
# With an array of [2]
http://localhost:15536/minorAreas?majorareas=2&_=1450307693166
# With an array of [2,3,9]
http://localhost:15536/minorAreas?majorareas=2&majorareas=3&majorareas=9&_=1450308261808
I've already looked at this question about passing arrays to a controller with a List<int> parameter, but the solution doesn't seem to work for me. What am I doing wrong?
GET? You might have more luck with aPOST.majorareas=new List<int>()in the route definition?