function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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!


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

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:pageBlockButtons >
                <apex:commandButton value="Submit" action="{!storeAnswers}"/>

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()) {
            we.add(new WrapperClassEx(cRef.qObj,selection));
        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()];
        noDuplicate = new Set<WrapperClassEx>();
        for(WrapperClassEx q : questions1) {
        display = new List<WrapperClassEx>();
        for(WrapperClassEx q1 : noDuplicate) {
        finalQuestions = new List<WrapperClassEx>();
        for(Integer i=0; i<5; 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() {
            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;