0

I am trying to access the values of a selected radio button from inside an apex:repeat. But when I try to get the values the values return null for the radio options. The visualforce code I have used is given below.

 <apex:repeat value="{!lstQuestions}" var="questions" id="theRepeat">
    <apex:panelGrid columns="3" frame="box" rules="rows" columnClasses="col1,col2,col3" id="theGrid">
        <apex:outputText > 1 </apex:outputText>
        <apex:outputText > {!questions.Question__c} </apex:outputText>
        <apex:selectRadio id="r1" styleClass="radio" label="" value="{!questions.Answer__c}">
            <apex:selectOptions itemLabel="Correct" ItemValue="Correct" />
            <apex:selectOptions itemLabel="Incorrect" ItemValue="Incorrect" />
        </apex:selectRadio>
    </apex:panelGrid>
</apex:repeat>

lstQuestions is of the object Feedback_Questions__c and contains instances of object. After selection when I iterate and check for the answers they are not loaded. Can someone please tell me what I am doing wrong?

  • What type is the Answer__c field? Maybe instead of use selectRadio you can try apex:inputField – Sergej Utko Feb 20 '15 at 8:40
  • Answer__c is a text field. And if I have apex:inputField then the user will be able to enter values, which I do not want as there are only 2 answers Correct or Incorrect. – user3197916 Feb 20 '15 at 10:09
  • what event on the page is causing the selected radio button value(s) to be sent to the controller? Does it have an action=immediate ? Have you looked to see if the setters on controller property lstQuestions are getting invoked – cropredy Feb 20 '15 at 19:34
1

Instead of using apex:selectOptions you have to use apex:selectOption tag:

    <apex:selectRadio id="r1" styleClass="radio" label="" value="{!questions.Answer__c}">
        <apex:selectOption itemLabel="Correct" ItemValue="Correct" />
        <apex:selectOption itemLabel="Incorrect" ItemValue="Incorrect" />
    </apex:selectRadio>

The apex:selectOptions tag need to have a merge field that references the controller class collection variable. And the apex:selectOption can be used without that collection.

| improve this answer | |
  • I changed it to apex:selectOption but still when I try to print getting the value for the field as null... – user3197916 Feb 20 '15 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.