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);
I banged my head against this wall for months with the regular .ajax() call.
I eventually found out that you need to set
traditional: true
in the params list for $.ajax(). (see the "traditional" heading here: http://api.jquery.com/jQuery.ajax/)Since there is no params list for $.post(), I'm not sure you can do this with $.post(). But it's not much more code to use $.ajax().
I had a similar problem when passing arrays.
Instead of using
$.post
use$.ajax
and set thetraditional
option =true
...The
traditional: true
option is importantThe following worked for me. You can use
serializeArray()serializeJSON() as below and set that to the data element. Observe the formData variable.