0

I am passing data to controller through Ajax call. Following is the ajax code:

var month_List = [];
$('#dojMonths :selected').each(function (i, selectedItem) {
  month_List[i] = $(selectedItem).text();
});
var from_Month = $("#fromKPAMonthPicker").val();
var from_Year = $("#fromKPAYearPicker").val();
var to_Month = $("#toKPAMonthPicker").val();
var to_Year = $("#toKPAYearPicker").val();
$.ajax({
  url: '/Home/_DataByFromTo',
  type: "POST",
  data: {
    doj_Month_List: month_List,
    from_Month: from_Month,
    from_Year: from_Year,
    to_Month: to_Month,
    to_Year: to_Year
  },
  dataType: "html",
  success: function (data) {
    $("#divList").html(data);
  }
});

Controller action method:

[HttpPost]
public ActionResult _DataByFromTo(List<Int32> doj_Month_List, Int16 from_Month, Int16 from_Year, Int16 to_Month, Int16 to_Year)
{
  return View();
}

It was working in my old code perfectly fine. I don't know whats the problem. because all data are passing perfectly except this array of jquery.

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
Dhwani
  • 7,484
  • 17
  • 78
  • 139

2 Answers2

1

To disable deep serialization of objects you need to set traditional property to true.

$.ajax({
    url: '/Home/_DataByFromTo',
    type: "POST",
    data: {
    doj_Month_List: month_List,
    from_Month: from_Month,
    from_Year: from_Year,
    to_Month: to_Month,
    to_Year: to_Year
    },
    dataType: "html",
    traditional: true,
    success: function (data) {
        $("#divList").html(data);
    }
});

When set to true it results in a shallow serialization. Following link might be of help. https://api.jquery.com/jQuery.param/

AbhinavRanjan
  • 1,638
  • 17
  • 21
0

try using push

var month_List = [];
$('#dojMonths :selected').each(function (i, selectedItem) {
    month_List.push($(selectedItem).text());
});
Matt Bodily
  • 6,403
  • 4
  • 29
  • 48