Reputation: 1095
I have 20 check boxes and a submit button on my MVC4 razor page. User can select multiple check boxes(or can leave all of them unchecked) and click on the submit button. How can I get the selected check box values (yes/no) to my controller using a jQuery ajax POST .
Upvotes: 1
Views: 1206
Reputation: 559
If you want to include "no" values as well for checkboxes, you might have to do something a little more hand-rolled; by default, serialize will not include checkbox values for ones that aren't checked, which can be seen in the jQuery documentation.
You could always iterate over the checkboxes with something like
$("input[type='checkbox']")
and use a for loop going over each one, including it in your JSON data to post to your server.
Upvotes: 1
Reputation: 1151
In jQuery, serialize() on a form will give you all the values. Then you just set this as the data in your post request.
var attachment = $('form#yourFormId').serialize();
Upvotes: 0