I have 2 dropdowns A and B.
Ais a normal dropdownBis a multiselect dropdown
A contains list of classes in a school and B contains divisions in a school
There is also a button for dynamic creation of these two dropdowns(There may be n dropdowns)
I want to save n dropdowns values simultaneously to my database but the problem is all my dynamically created field attribute names are same as the original two dropdowns, this way I am getting the value of last dropdown entered to my backend. I can dynamically change the attribute names using js, but I can't dynamically create variables in my dto.
I want my n dropdown values to bind into same DTO variables as a list or something like that, Is there any other methods to achieve this?
i researched and went up with apache commons collections4 and autopopulatingList but i don't find any proper examples
My DTO class
public class TestDto {
private Long fkcl;
private String[] fkdiv;
public Long getFkcl() {
return fkcl;
}
public void setFkcl(Long fkcl) {
this.fkcl = fkcl;
}
public String[] getFkdiv() {
return fkdiv;
}
public void setFkdiv(String[] fkdiv) {
this.fkdiv = fkdiv;
}
ClassDes
public class ClassDes {
public List<TestDto> list = new ArrayList<TestDto>();
public List<TestDto> getList() {
return list;
}
public void setList(List<TestDto> list) {
this.list = list;
}
}
Controller
@RequestMapping(value = "/testing")
public ModelAndView ff(Model model) {
ClassDes testprof = new ClassDes();
List<ClassMaster> bslist = serv.findAllclass();
model.addAttribute("blah", bslist);
List<StudentMaster> std = stdServ.findAll();
model.addAttribute("std", std);
return new ModelAndView("test" , "testprof", testprof);
}
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView save(@ModelAttribute ClassDes testprof) {
System.out.println(BasicGson.toGson(testprof));
return new ModelAndView("redirect:/testing", "testprof", testprof);
}
but my list returns empty {"list":[]} but in my ajax method,
it shows
{"listed[].fkcl":"3","listed[].fkdiv":["1","2","3","4","5"]}