Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery post() with serialize and array of data

i am unable to get collection value during post in mvc 3. it is returning null.

$.post("/Work/Post", { vm: $('#myForm').serializeArray(), 'collection': ['a', 'b', 'c'] });

//Or


var data = $('#myForm').serializeArray();
data.push({ name: 'collection', value: ['a', 'b', 'c'] });
$.post("/Work/Post", data);

//Or

var data = $('#myForm').serializeArray();
data.push({ name: 'collection[]', value: ['a', 'b', 'c'] });
$.post("/Work/Post", data);
like image 405
Thulasiram Avatar asked Aug 08 '12 15:08

Thulasiram


1 Answers

I had a similar problem when passing arrays.

Instead of using $.post use $.ajax and set the traditional option = true ...

$.ajax({
    type: "POST",
    url: "Work/",
    traditional: true,
    data: { collection: ['a','b','c'] }
});

The traditional: true option is important

like image 194
MattW Avatar answered Sep 30 '22 04:09

MattW