我有一个包含ManyToMany关系的模型:
@JoinTable(name = "Contract_has_Institution", joinColumns = { @JoinColumn(name = "contract_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "institution_id", referencedColumnName = "id") })
@ManyToMany
public List<Institution> institutionCollection;
在scala.html中我有两个选择,在列表中添加两个元素(两个Insitutions)
@select(contractForm("institutionCollection"),
options = options(InstitutionController.options),
'_default -> "--- Choose an institution ---",
'_label -> "Institution A",
'_showConstraints -> false)
@select(contractForm("institutionCollection"),
options = options(InstitutionController.options),
'_default -> "--- Choose an institution ---",
'_label -> "Institution B",
'_showConstraints -> false)
我尝试将[]添加到这样的字段
contractForm("institutionCollection[]"),
但它不起作用。 如何将两个选定的选项绑定到列表中?
答案 0 :(得分:2)
在您看来,如果您使用当前代码:
@select(contractForm("institutionCollection"),
options = options(InstitutionController.options),
'_default -> "--- Choose an institution ---",
'_label -> "Institution A",
'_showConstraints -> false)
@select(contractForm("institutionCollection"),
options = options(InstitutionController.options),
'_default -> "--- Choose an institution ---",
'_label -> "Institution B",
'_showConstraints -> false)
因此,您可以使用此代码获取多次选择的值(这只是示例):
public static Result someAction() {
Map<String,String[]> formData = request().body().asFormUrlEncoded();
Institution institution;
// get all institutionCollection select value
for (String insId : formData.get("institutionCollection")) {
Logger.debug("INSTITUTION SELECTED = " + insId); // log info
// I assume that select value is the ID of each Institution record
institution = Institution.find.byId(Long.parseLong(insId))
SomeModel.institutionCollection.add(institution) // add to the list to your model
}
SomeModel.save(); // save changes
return ok("success");
}