+ Start a Discussion
College ManagementCollege Management 

How can i get selectOption value from PageBlockTable to controller?

I've written a wrapperClass to display few questions in VFPage and i added an Answer field as selectList by giving few options to it through apex class. But while submitting , i am getting the last given option for every answer.Please help me out!

Thanks,!
Sure@DreamSure@Dream
Hi,


Can you post the code snippets.
That would be helpful.

Thanks
College ManagementCollege Management
VFPage :

<apex:page controller="WrapperTest" sidebar="false" tabStyle="Account" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!wrapperObj}" var="w">
                <apex:column >
                    <apex:pageBlockSection title="{!w.qObj.Name}" >
                        <apex:outputField value="{!w.qObj.Question__c}"/><br/>
                        <apex:outputField value="{!w.qObj.Option1__c}"/>
                        <apex:outputField value="{!w.qObj.Option2__c}"/>
                        <apex:outputField value="{!w.qObj.Option3__c}"/>
                        <apex:outputField value="{!w.qObj.Option4__c}"/>
                        <apex:selectList value="{!selection}" size="1">
                            <apex:selectOptions value="{!w.Answers}" />
                        </apex:selectList>
                    </apex:pageBlockSection>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Submit" action="{!storeAnswers}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Apex Class :

public class WrapperTest {

    public List<WrapperClassEx> wrapList{get;set;}
    public List<WrapperClassEx> questions1{get; set;}
    public WrapperClassEx theLuckyUser {get; set;}
    public Set<WrapperClassEx> noDuplicate {get; set;}
    public List<WrapperClassEx> display { get; set;}
    public List<WrapperClassEx> finalQuestions { get; set;}
    public String selection {get; set;}
 
    public PageReference storeAnswers() {
        List<WrapperClassEx> we = new List<WrapperClassEx>();
        for(WrapperClassEx cRef : getWrapperObj()) {
            System.debug('----------'+selection);
            we.add(new WrapperClassEx(cRef.qObj,selection));
         
        }
        System.debug('***********'+we);
        return null;
    }
 
    public List<WrapperClassEx> getWrapperObj() {
 
        wrapList = new List<WrapperClassEx>();
        for(Question__c q : [SELECT Name, Id, Question__c, Option1__c, Option2__c, Option3__c, Option4__c, Answer__c FROM Question__c ORDER BY Name ASC LIMIT 15]) {
            wrapList.add(new WrapperClassEx(q,selection));
        }
        questions1 = new List<WrapperClassEx>();
        for(Integer i=0; i< wrapList.size();i++) {
            Double theChosenOne = Math.round(math.random()*(wrapList.size()-1));
            theLuckyUser = wrapList[theChosenOne.intValue()];
            questions1.add(theLuckyUser);
        }
     
        noDuplicate = new Set<WrapperClassEx>();
        for(WrapperClassEx q : questions1) {
            noDuplicate.add(q);
        }
     
        display = new List<WrapperClassEx>();
        for(WrapperClassEx q1 : noDuplicate) {
            display.add(q1);
        }
     
        finalQuestions = new List<WrapperClassEx>();
        for(Integer i=0; i<5; i++) {
            finalQuestions.add(display[i]);
        }
     
        return finalQuestions;
    }

    public class WrapperClassEx {
        public Question__c qObj {get; set;}
        public SelectOption[] Answers {set;}
        public String selectedValue {get; set;}
     
        public WrapperClassEx(Question__c q, String s) {
            this.qObj = q;
            this.selectedValue = s;
        }

        List<SelectOption> so = new List<SelectOption>();
        public List<SelectOption> getAnswers() {
            so.clear();
            List<String> answers = new List<String>{'Option 1', 'Option 2', 'Option 3', 'Option 4'};
            for(String ans : answers) {
                so.add(new SelectOption(ans,ans));
            }
            return so;
        }
    }
}

Thanks!