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
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11 

Couldn't get the select list value from visualforce page to controller

VFP:

<apex:page standardController="Teacher__c" extensions="RGMTeacherController">   
      <apex:form id="loginForm" forceSSL="true">
        
        <apex:outputPanel id="actualblock" >
            <apex:tabPanel switchType="client" tabClass="activeTab" inactiveTabClass="inactiveTab" selectedTab="Information">
                
                <apex:tab id="home" label="Home" title="Home">                   
                </apex:tab>
                
                <apex:tab label="Information" name="Information" id="InformationTab" >                                   ----------------------------------------                    
                </apex:tab>

                <apex:tab id="student" label="Student" title="Student">
                    <apex:pageBlock >
                        <apex:selectList label="Standard" id="choosestandard" required="true" value="{!standard}" >
                            <apex:selectOptions value="{!standarditems}" id="standarditem" />                           
                        </apex:selectList> 
                        <apex:selectList label="Section" id="choosesection" required="true" value="{!section}" >
                            <apex:selectOptions value="{!sectionitems}" id="sectionitem" />                           
                        </apex:selectList>
                        <apex:selectList label="Subject" id="choosesubject" required="true" value="{!subject}" >
                            <apex:selectOptions value="{!subjectitems}" id="subjectitem" />                           
                        </apex:selectList>
                        <apex:commandButton action="{!studentsearch}" value="Search" reRender="stulistid" />           
                        <apex:outputPanel id="stulistid" >
                        <apex:pageblocktable value="{!studentlist}" var="a" id="pbTable" >
                            <apex:column value="{!a.Name}"/>
                            <apex:column value="{!a.Roll_Number__c}"/>
                        </apex:pageblocktable>
                        </apex:outputPanel>
                    </apex:pageBlock>
                </apex:tab>
               
            </apex:tabPanel>
        </apex:outputPanel>
      </apex:form>
       
</apex:page>


CTRL:

public class RGMTeacherController {
    public Boolean showRecords{get;set;}
    public List<Student__c> studentlist{get;set;}
    public string[] standard{get;set;}
    public string[] section{get;set;}
    public string[] subject{get;set;}
    public string[] sections;
    public string[] standards;
    public string[] subjects;
     
    public RGMTeacherController(ApexPages.StandardController controller) {
 
        Teacher__c TeacherId = [SELECT Id, Name, Email__c, Section__c, Standard__c, Subject__c, Contact_Number__c FROM Teacher__c WHERE Username__c =: username AND Password__c =: password LIMIT 1];
        ApexPages.currentPage().getParameters().put('id', TeacherId.Id);
        sections = TeacherId.Section__c.split(';', 7);
        standards = TeacherId.Standard__c.split(';', 7);
        subjects = TeacherId.Subject__c.split(';', 7);
    }
    public List<SelectOption> getstandarditems() {
        List<SelectOption> standardoptions = new List<SelectOption>();
         for(Integer i=0; i < standards.size(); i++) {
             standardoptions.add(new SelectOption(standards[i], standards[i]));
         }
        return standardoptions;
    }
    
    public String[] getstandard() {
        return standard;
    }
            
    public void setstandard(String[] standard) {
        this.standard = standard;
    }
    
    public List<SelectOption> getsectionitems() {
        List<SelectOption> sectionoptions = new List<SelectOption>();
        for(Integer i=0; i < standards.size(); i++) {
             sectionoptions.add(new SelectOption(sections[i], sections[i]));
         }
        return sectionoptions;
    }
    
    public String[] getsection() {
        return section;
    }
            
    public void setsection(String[] section) {
        this.section = section;
    }
    
    public List<SelectOption> getsubjectitems() {
        List<SelectOption> subjectoptions = new List<SelectOption>();
        system.debug('*********************************'+standard);
        for(Integer i=0; i < subjects.size(); i++) {
             subjectoptions.add(new SelectOption(subjects[i], subjects[i]));
         }
        return subjectoptions;
    }
    
    public String[] getsubject() {
        return subject;
    }
            
    public void setsubject(String[] subject) {
        this.subject = subject;
    }
    
    public void studentsearch() {
        system.debug('*********************************'+standard);
        studentlist = [SELECT Id, Name, Roll_Number__c FROM Student__c WHERE Standard__c =: standard AND Section__c =: section];
        system.debug('*********************************'+studentlist);
        
    }   
    
}
Best Answer chosen by Vijay Kumar Rebbala 11
shikher goelshikher goel
Hi Vijay,

Well variable declaration is wrong here thats why you are not able to get the values of SelectList in controller. If you are using multiselect=true attribute on apex:selectList tag,then we define the String[] at controller level. But since you had not used multiselect then you need to define the String variable in controller.

Standard,section and subject variable types should be String instead of String[].

You can update your controller code like this

CTRL:

public class RGMTeacherController {
    public Boolean showRecords{get;set;}
    public List<Student__c> studentlist{get;set;}
    public string standard{get;set;}
    public string section{get;set;}
    public string subject{get;set;}
    public string[] sections;
    public string[] standards;
    public string[] subjects;
     
    public RGMTeacherController(ApexPages.StandardController controller) {
 
        Teacher__c TeacherId = [SELECT Id, Name, Email__c, Section__c, Standard__c, Subject__c, Contact_Number__c FROM Teacher__c WHERE Username__c =: username AND Password__c =: password LIMIT 1];
        ApexPages.currentPage().getParameters().put('id', TeacherId.Id);
        sections = TeacherId.Section__c.split(';', 7);
        standards = TeacherId.Standard__c.split(';', 7);
        subjects = TeacherId.Subject__c.split(';', 7);
    }
    public List<SelectOption> getstandarditems() {
        List<SelectOption> standardoptions = new List<SelectOption>();
         for(Integer i=0; i < standards.size(); i++) {
             standardoptions.add(new SelectOption(standards[i], standards[i]));
         }
        return standardoptions;
    }
    
    public String getstandard() {
        return standard;
    }
            
    public void setstandard(String standard) {
        this.standard = standard;
    }
    
    public List<SelectOption> getsectionitems() {
        List<SelectOption> sectionoptions = new List<SelectOption>();
        for(Integer i=0; i < standards.size(); i++) {
             sectionoptions.add(new SelectOption(sections[i], sections[i]));
         }
        return sectionoptions;
    }
    
    public String getsection() {
        return section;
    }
            
    public void setsection(String section) {
        this.section = section;
    }
    
    public List<SelectOption> getsubjectitems() {
        List<SelectOption> subjectoptions = new List<SelectOption>();
        system.debug('*********************************'+standard);
        for(Integer i=0; i < subjects.size(); i++) {
             subjectoptions.add(new SelectOption(subjects[i], subjects[i]));
         }
        return subjectoptions;
    }
    
    public String getsubject() {
        return subject;
    }
            
    public void setsubject(String subject) {
        this.subject = subject;
    }
    
    public void studentsearch() {
        system.debug('*********************************'+standard);
        studentlist = [SELECT Id, Name, Roll_Number__c FROM Student__c WHERE Standard__c =: standard AND Section__c =: section];
        system.debug('*********************************'+studentlist);
        
    }   
    
}

Hope this helps.

Thanks,
Shikher Goel

All Answers

shikher goelshikher goel
Hi Vijay,

Well variable declaration is wrong here thats why you are not able to get the values of SelectList in controller. If you are using multiselect=true attribute on apex:selectList tag,then we define the String[] at controller level. But since you had not used multiselect then you need to define the String variable in controller.

Standard,section and subject variable types should be String instead of String[].

You can update your controller code like this

CTRL:

public class RGMTeacherController {
    public Boolean showRecords{get;set;}
    public List<Student__c> studentlist{get;set;}
    public string standard{get;set;}
    public string section{get;set;}
    public string subject{get;set;}
    public string[] sections;
    public string[] standards;
    public string[] subjects;
     
    public RGMTeacherController(ApexPages.StandardController controller) {
 
        Teacher__c TeacherId = [SELECT Id, Name, Email__c, Section__c, Standard__c, Subject__c, Contact_Number__c FROM Teacher__c WHERE Username__c =: username AND Password__c =: password LIMIT 1];
        ApexPages.currentPage().getParameters().put('id', TeacherId.Id);
        sections = TeacherId.Section__c.split(';', 7);
        standards = TeacherId.Standard__c.split(';', 7);
        subjects = TeacherId.Subject__c.split(';', 7);
    }
    public List<SelectOption> getstandarditems() {
        List<SelectOption> standardoptions = new List<SelectOption>();
         for(Integer i=0; i < standards.size(); i++) {
             standardoptions.add(new SelectOption(standards[i], standards[i]));
         }
        return standardoptions;
    }
    
    public String getstandard() {
        return standard;
    }
            
    public void setstandard(String standard) {
        this.standard = standard;
    }
    
    public List<SelectOption> getsectionitems() {
        List<SelectOption> sectionoptions = new List<SelectOption>();
        for(Integer i=0; i < standards.size(); i++) {
             sectionoptions.add(new SelectOption(sections[i], sections[i]));
         }
        return sectionoptions;
    }
    
    public String getsection() {
        return section;
    }
            
    public void setsection(String section) {
        this.section = section;
    }
    
    public List<SelectOption> getsubjectitems() {
        List<SelectOption> subjectoptions = new List<SelectOption>();
        system.debug('*********************************'+standard);
        for(Integer i=0; i < subjects.size(); i++) {
             subjectoptions.add(new SelectOption(subjects[i], subjects[i]));
         }
        return subjectoptions;
    }
    
    public String getsubject() {
        return subject;
    }
            
    public void setsubject(String subject) {
        this.subject = subject;
    }
    
    public void studentsearch() {
        system.debug('*********************************'+standard);
        studentlist = [SELECT Id, Name, Roll_Number__c FROM Student__c WHERE Standard__c =: standard AND Section__c =: section];
        system.debug('*********************************'+studentlist);
        
    }   
    
}

Hope this helps.

Thanks,
Shikher Goel
This was selected as the best answer
Vijay Kumar Rebbala 11Vijay Kumar Rebbala 11
Thanks Shikher.