[英]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. Jobads和Jobcategory是我的模型。
My Jabads.java model 我的Jabads.java模型
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 我的问题是,每当我提交带有复选框的视图表单时(上面给出)。我的控制台打印的长度为: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. 并将我的表单提交给控制器,在控制器的第一行将生成[[NumberFormatException:对于输入字符串:“ index”]]。
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.