I'm looking for a way to store the values of all existing checkboxes of a given class into some kind of list or array and send the result via jquery to an asp.net mvc controller. The checkboxes do not have to be checked, I need all of them.
More details:
My checkboxes look like this
<input type="checkbox" value="1" class="a-checkbox"
<input type="checkbox" value="2" class="a-checkbox"
<input type="checkbox" value="3" class="a-checkbox"
It would be nice if my MVC controller could look like this
public JsonResult SaveList(List<String> values) { //... }
I know that I could access the checkboxes in the following way
$('input.a-checkbox').each(
function () {
// create the list with the help of $(this).val()
}
);
// jquery post would happen here
But I dont know how to create such a data structure. Could you help me?
Thank you
edit: nice, thanks. Can you tell me whats wrong with this? My controller gets called indeed, but the list is null (on server side)
var list = [];
$('a-checkbox').each(
function () {
list.push($(this).val());
}
);
$.ajax({
type: "POST",
url: myUrl,
data: list,
success: function (data) {
alert(data.Result);
},
dataType: "json",
traditional: true
});