4

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
            });
1
  • If the values are null it means you have a model binding error. Here's an extension method I use to dump bind errors to the console in debug: pastebin.com/S0gM3vqg Commented Dec 8, 2011 at 15:27

4 Answers 4

3

This will put all the checkbox values (value attributes) into an Array:

var values = [];
$(".a-checkbox").each(function () {
    values.push($(this).val());
});

// values now equals ["1", "2", "3"]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks to all of you, maybe you could take a look at m edit above? Have problems with the post
jQuery will serialise your list using param. There is an example on how Arrays are serialised on the linked page.
1

Try this

var list = [];
$('input.a-checkbox').each(
    function () {
        // create the list with the help of $(this).val()
        list.push($(this).val());
    }
);

Now you can post list object to your mvc controller action.

Comments

1

I use .Serialize() to format form data for my mvc ajax actions.

var checkPostData = $(".a-checkbox").serialize();

http://api.jquery.com/serialize/

2 Comments

This produces a querystring (ex: a=1&b=2&c=3&d=4&e=5), not an array.
Well he did say "It would be nice..." not "It is required" :P The mvc model binder handles the details just fine usually. Your right might not be what the OP is after though.
0

I had the same issue Nulls in my MVC mode and all. I found using $("input:checked") as my data instead of trying to extract the values to an array in js worked better and I fixed the Nulls issue by giving my checkboxes name values because MVC binds on input names.

HTML

<a class="post-checkboxes" href="#">post checkboxes</a>
<input name="[MVC viewmodel list variable name]" type="checkbox" value="1" />
<input name="[MVC viewmodel list variable name]" type="checkbox" value="2" />

Javascript

$('.post-checkboxes').click(function (e) {
    e.preventDefault();
    $.post("[location to post to]", $("input:checked"));            
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.