Friday, March 11, 2016

How to pass javascript array to C# code behind

Used jquery version: jquery-1.8.3.min.js

html(cshtml) file
function updateNotification() {
    var ids = [1, 2, 3, 4, 5];

    if (ids.length > 0) {
        $.ajax({
            traditional: true,             // Should be true
            type: "POST",
            url: "/Notification/Update",   // Url to pass
            data: { ids: ids },
            dataType: "json",
            success: function (result) {
            },
            error: function (result) {
            }
        });
    }
}

Code Behind(cs) file
        [HttpPost]
        public JsonResult Update(int[] ids)
        {
            // ids is int array

            return Json(new { result = true });
        }