How to pass select option value selected to an object in the form

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to pass select option value selected to an object in the form

David
Hi.
I show a form with several input text fields and a select option.
Everything works great, except I cannot pass the value selected in select option to save the form, it give me an empty value.


This is part of the code, it has just the select option.

<form class="form-horizontal" th:object="${directory}" th:action="@{/dashboard/directory/save}" method="POST">



                                <label class="col-sm-2 control-label">Cod Category*</label>
                                <div class="col-md-10" th:field="*{category.cod_category}">
                               
                                        <div th:object="${categories}">
                                                <select class="custom-select" id="categories" name="categories">
                                                        <option th:each="mycategory : ${categories}"
                                                                th:value="${mycategory.cod_category}"
                                                                th:selected="${mycategory.cod_category == directory.category.cod_category}"
                                                                th:text="${mycategory.name}"></option>
                                                </select>
                                        </div>

                                </div>


</form>

This is the main table
Table Directory
id
cod_category
date
extra fields


Table Category
cod_category
name

In my controller I pass two objects, directory and categories. categories is the list.

model.addAttribute("directory", directoryService.findByCode(code););
model.addAttribute("categories", categoryService.findAll());

Any suggestions?
Thanks