tldr
tldr

Reputation: 12112

Play Framework - sending data from view to controller

Controller can send data to view as argument in the render method. How would the view send data to the controller?

Edit: Let me clarify it a bit.

Application.showQuestion(Question q){ render(q); }

showQuestion.html displays the question 'q' and has a text box for entering the answer 'a' to 'q'. The submit button on the form calls the method createAnswer. I want to pass in not only the answer 'a', but also the question 'q'. Would createAnswer(q, a) accomplish the same? My objective is to do:

a.question = q; q.addAnswer(a);

Upvotes: 0

Views: 2605

Answers (2)

kheraud
kheraud

Reputation: 5288

I guess that what you are trying to do is to let the user call the right controller with a click on your generated webpage.

What you are looking for is called reverse routing, Play has a mechanic for that. You can see how it works here. Then If you are trying to get input from a form I would recommend you to use the Form object as detailed in the documentation

Upvotes: 1

Břetislav Wajtr
Břetislav Wajtr

Reputation: 338

I'm not entirely sure if I understand your question correctly, but hopefully yes:

The easiest way how to pass your question q along with answer a is using predefined template "form". You can do something like:

showQuestion.html:

#{form @Application.createAnswer(q), id:'answerForm'}
   ...
   // add input for the answer here
   ...
#{/form}

Then in your Application.java you would define method Application.createAnswer(q, a), which will be called. So the arguments used in the form tag and the inputs in the form are combined together. What the part #{form @Application.createAnswer(q)... actualy only does is, that it creates hidden input fields in the form, which are passed to the controller, when submit button is clicked.

Details about the form tag: Form tag in playframework 1.2.4

Please note that what I described applies to playframework 1.2.4. I'm not sure if it works same way in 2.0.1, but I (really) hope yes.

Upvotes: 1

Related Questions