Pass the list of checked values to controller by Bean object?

Viewed 65

Trying to pass the list of selected subjects to Controller, but the bean is not receiving the list of subjects.

Here's the code that I'm trying:

$("#submitCreateStudent").click(function() {
      var selSubjectIds = new Array();
      $('#subjectsTableInCreateStudent').find('.jtable-data-row').each(function() {
        if (selSubjectIds.length < 1) {
          selSubjectIds.push($(this).attr('data-record-key') + '$');
        } else {
          selSubjectIds += $(this).attr('data-record-key') + '$';
        }
      });

      if (validateCreateStudentForm()) {

        var data = new FormData();
        data.append('firstName', $("#firstNameInCreateStudent").val());
        data.append('lastName', $("#lastNameInCreateStudent").val());
        data.append('address', $("#studentAddressInCreateStudent").val());
        data.append('collegeName', $("#studentCollegeNameInCreateStudent").val());
        data.append('phoneNumber', $("#phoneNumberInCreateStudent").val());
        data.append('email', $("#emailInCreateStudent").val());
        data.append('studentDepartment', $("#departmentInCreateStudent").val());
        data.append('studentBranch', $("#branchInCreateStudent").val());
        data.append('DateOfBirth', $("#dobInCreateStudent").val());
        data.append('DateOfReport', $("#dorInCreateStudent").val());
        data.append('loginUserName', $("#studentUserNameInCreateStudent").val());
        data.append('loginPassword', $("#passwordInCreateStudent").val());
        data.append('studentSubjects', selSubjectIds);

        //                        data.append('skipInitialTraining', skipInitialTraining);

        $("#maskingId").mask("Saving...");

        $.ajax({
          url: "AddStudent.do",
          data: data,
          //                           
          //Remaining code goes here.
        });
      });

All the details are coming to controller class except for the list of subjects which are selected check boxes.

2 Answers

By default, jQuery supports json format submissions

data :{‘selSubjectIds’:selSubjectIds} or $(this).serialize();

Thanks to Everyone for your efforts.I found my self a way to solve this as Follows

var arr = "";
$('#subjectsTableInCreateStudent').find('.jtable-data-row').each(function() {
                        var record = $(this).data('record');
                        arr += record.subjectId + seperatorForGridCells;   

                    });

Here the arr variable stores every ID as a String by concating with Some seperator "seperatorForGridCells". Then i used StringTokenizer to seperate each id and parsed each id from string type to int.

Related