简体   繁体   中英

sending a list of checked boxes from view to controller in play framework java

I have a view with the checkboxes

 @for((jc)<-jobcategoryList) {

           <input type="checkbox" name="jobcategory.id" value="@jc.id">@jc.name<br>
                                }

I just want to send this list of checked checkboxes to my controller.But on doing this

public class JobAdController extends Controller {
    public static Result save() {


            Form<Jobads> jobadsFormData = jobadsForm.bindFromRequest();



            if (jobadsFormData.hasErrors()) {

          System.out.println("Error in form");
                 return badRequest();


            } else {

            Jobads jads= jobadsFormData.get();
            List<Jobcategories> jadsList= jads.getJobcategory();

            System.out.print("\nLength is:"+jadsList.size());// always prints Length is:0





          }}
}

where Jobads and Jobcategory are my models.

My Jabads.java model

public class Jobads extends Model {


    @ManyToMany

    private List<Jobcategories> jobcategories;
}

My problem is that whenever i submit my view form with the checkboxes(given above).My console prints Length is:0

When i tried to change my view to

@for((jc,index)<-jobcategoryList.zipWithIndex) {

               <input type="checkbox" name="jobcategory[index]" value="@jc">@jc.name<br>
                                    }

and submit my form the controller an [[NumberFormatException: For input string: "index"]] is generated on the first line of controller.

How can I send this checked boxes to my controller.

Thanks

Use:

@for(jc <- jobcategoryList) {
    <input type="checkbox" name="jobcategories[]" value="@jc.id">@jc.name<br>
}

or

@for((jc,index) <- jobcategoryList.zipWithIndex) {
    <input type="checkbox" name="jobcategories[@index]" value="@jc.id">@jc.name<br>
}

and in your form class:

public List<Long> jobcategories;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM