如何从下拉框中获取用户选择的值并将其添加到模型中? [英] How to I get the user selected value from a drop-down box and add it to the model?

查看:106
本文介绍了如何从下拉框中获取用户选择的值并将其添加到模型中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,用户可以在其中创建新的考试,并且用户可以从下拉菜单中选择一个主题。此下拉列表包含主题字符串,而不是实际主题对象。在我的程序中,实际的主题对象与考试具有一对多的关系。

I have a form where the user is creating a new exam and in the form the user selects a subject from a drop down menu. This drop down contains strings of subjects not actual subject objects. In my program there are actual subject objects which have a one to many relationship with exam.

我如何找到用户选择的值?我想将其添加到模型中,以便创建检查对象并将其添加到数据库。

How do I find what value the user selected? I want to add it to the model so that I can create an exam object and add it to the database.

将用户选择的主题字符串添加到模型如何
如何找到用户选择的值?
我想将他们选择的内容设置为与科目名称相同,然后在科目存储库
中搜索具有相同名称的科目,以便可以将考试
添加到该科目。

Once the subject string that the user selected is added to the model how do I find the value the user selected? As I want to set what they selected to equal subject Name and then search the subject repository for a subject with the same name so that the exam can be added to that subject.

希望我的代码可以使这一点更加清楚。

Hopefully my code will make this a bit more clear.

我收到此错误。 Bean名称主题的绑定结果和普通目标对象均不能用作请求属性。

I am getting this error. Neither Binding Result nor plain target object for bean name subject available as request attribute.

此代码来自我的控制器

@GetMapping("/addexam")
public String showExamForm(Model model) {

    // Here I am finding the subjects that belong to the current user 

  //and adding them as strings to an arraylist. 


  //I populate the dropdown with strings fromthis arraylist.        


    Authentication loggedInUser = SecurityContextHolder.getContext().getAuthentication();
    String email = loggedInUser.getName();   

    User user = userRepository.findByEmailAddress(email);

    ArrayList<String> subjects = new ArrayList<String>();

    for(Subject sub:user.getSubject())
    {
        subjects.add(sub.getSubjectName());
    }
        model.addAttribute("subjects", subjects);

return "addExam";
}

@PostMapping("/addexam")
public String addNewExam(@ModelAttribute("exam") @Valid @RequestBody Exam 
 exam,UserRegistrationDto userDto, BindingResult result, Model model) {

    examRepository.save(exam);
    model.addAttribute("examTitle", exam.getExamTitle());
    model.addAttribute("examGradeWorth", exam.getExamGradeWorth());
    model.addAttribute("subject", exam.getSubject()); 


    //I want to find the user selected value and set it to equal 
    //subjectname:
    String subjectName =;

//Here I will search the subjectRepository for the subjectName and set subject to equal the subject that was found.

    Subject subject = subjectRepository.findBySubjectName(subjectName);
 //then exam will be added to that subject as subject has a one to many relationship with exam.

    subject.addExam(exam);
subjectRepository.save(subject);

return "userProfile1";


}
 }

html。

<form action="#" th:action="@{/addExam}" th:object="${exam}" 
         method="post">
                     <div th:object="${subject}">
            <select th:field="*{subject}" class="form-control" id="subject"             
   name= "subject">
            <option value="">Select subject</option>

            <option 
                th:each="Subject : ${subjects}" 
                th:value="${Subject}" 
                th:text="${Subject}"></option>
         </div>
         <div>
                <table>
                    <tr>

                     <td><input type="text" th:field="*{examTitle}" /></td>

                    </tr>  
                    <tr>
                        <td> Exam grade worth </td>
                        <td><input th:field="*{examGradeWorth}" /></td>

                        </tr>  


推荐答案

我认为您不需要使用两个 th:object 。只需使用 th:value

I don't think you need to use two th:object. Just use th:value.

例如,在下一个示例中,您将发送两个对象并设置<$ 学生对象中的c $ c>名称和中的状态考试对象。 (这不是您的模型的表示。只是一个例子。)

For instance, in the next example you are sending two objects and setting name in student object and status in exam object. (It isn't a representation of your model. It's just an example).

<form th:action="@{/addExam}" method="post">
      <input type="text" th:value="${student.name}" name="name"/>
      <input type="text" th:value="${exam.status}" name="status"/>
      <button type="submit">Go</button>
</form>

th:object 如果有一堆 exam 字段。

请记住,字段名称必须与bean匹配字段名称。

Keep in mind that the name of the fields have to match the bean field name.

因此,您的HTML或多或少看起来像这样:

So, your HTML will looks like this more or less:

<form action="#" th:action="@{/addExam}" method="post">
      <div>
          <select th:field="*{subject}" class="form-control" id="subject" name="subject">
            <option value="">Select subject</option>

            <option 
                th:each="Subject : ${subjects}" 
                th:value="${Subject}" 
                th:text="${Subject}"></option>
         </div>
         <div>
                <table>
                    <tr>

                     <td><input type="text" th:field="*{exam.examTitle}" /></td>

                    </tr>  
                    <tr>
                        <td> Exam grade worth </td>
                        <td><input th:field="*{exam.examGradeWorth}" /></td>

                        </tr>  

让我知道它是否有效!。

Let me know if it works!.

这篇关于如何从下拉框中获取用户选择的值并将其添加到模型中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆