Im trying to send a array to my method with $.post() but Im kinda lost how to go about this.
I am getting the values of the checkboxes thats outside my form and pass them into my method.
I tried to use .serializeArray() & .serialize() but get the error that
list.serializeArray is not a function
I i just send in the array i get null value.
HTML
<form....><input type="submit" value="submit" id="click_post" /></form>
<div id="container">
<input type="checkbox" value="2,3" name="checkbox" />
<input type="checkbox" value="4,5" name="checkbox" />
<input type="checkbox" value="6,7" name="checkbox" />
</div>
JQUERY
$('#click_post').click(function () {
var list = new Array();
$('#container input[type="checkbox"]').each(function () {
list.push($(this).val());
});
var $form = $(this).closest('form');
$.post($form.attr('action'), { value: list }, function (data) {
alert('test');
});
});
METHOD
public ActionResult PassArray(string[] value){//doing some logic here}
$(‘#click_post’).click(function () {
var $form = $(this).closest(‘form’);
$.post($form.attr(‘action’), $form.serialize(), function (data) {
alert(‘test’);
});
});
and also change the name of your action parameter to
checkbox(because that’s what you used as name for the checkboxes in the form):UPDATE:
or if you wanted to send only the values of the checkboxes and not any other form input elements you might need to use the
traditionalmode:or:
and then: