+ Start a Discussion

Passing selected picklist value (from inputField) to controller



Is it possible to pass the user selected picklist value from an inputField to a controller? If so, can someone either post a snippet or pseudo code of how this would be achieved?


Thanks in advance.

Ron HessRon Hess

This code will pass/bind the value, setting and getting values from visualforce to Apex is called binding



<!-- Page: --> <apex:page controller="sampleCon"> <apex:form> <apex:selectList value="{!countries}" multiselect="true"> <apex:selectOptions value="{!items}"/> </apex:selectList><p/> <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/> </apex:form> <apex:outputPanel id="out"> <apex:actionstatus id="status" startText="testing..."> <apex:facet name="stop"> <apex:outputPanel> <p>You have selected:</p> <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList> </apex:outputPanel> </apex:facet> </apex:actionstatus> </apex:outputPanel> </apex:page> /*** Controller: ***/ public class sampleCon { String[] countries = new String[]{}; public PageReference test() { return null; } public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('US','US')); options.add(new SelectOption('CANADA','Canada')); options.add(new SelectOption('MEXICO','Mexico')); return options; } public String[] getCountries() { return countries; } public void setCountries(String[] countries) { this.countries = countries; } }




Thanks for the quick response Ron. Is it possible though to bind values through the inputField tag (instead of selectList tag)? Since the field I'm working with is of type picklist and I'd like to present the picklist values within a drop down.

Here's what I've worked on so far. I don't know if it is possible to bind data using the inputField tag, so I used a selectList instead. I'm still not able to properly bind data from VF, so any suggestions will be appreciated. All I'm trying to do here is to have the user select a value from the picklist and then pass that selected value to my controller. As you can tell, I'm fairly new to VF, so please bear with me :P


VF Page:


<apex:page controller="ZipCMQuery"> <style> .activeTab {background-color: #236FBD; color:white; background-image:none} .inactiveTab { background-color: lightgrey; color:black; background-image:none} </style> <apex:tabPanel switchType="client" selectedTab="IXI_DT" id="IXITabPanel" tabClass="activeTab" inactiveTabClass="inactiveTab"> <apex:tab label="Search" name="IXI_Search" id="IXI_Search"> <apex:form > <apex:selectList size="1" value="{!zones}"> <apex:selectOptions value="{!Items2}"/> </apex:selectList> <p/> <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/> </apex:form> <apex:outputPanel id="out"> <apex:actionstatus id="status" startText="testing..."> <apex:facet name="stop"> <apex:outputPanel > <p>You have selected:</p> <apex:dataList value="{!zones}" var="c">{!c}</apex:dataList> </apex:outputPanel> </apex:facet> </apex:actionstatus> </apex:outputPanel> </apex:tab> </apex:tabPanel> </apex:page>






public class ZipCMQuery { String[] zones = new String[]{}; public PageReference test() { return null; } public List<SelectOption> getItems2() { List<SelectOption> options = new List<SelectOption>(); List<String> zones = new List<String>(getZoneR()); for (Integer i=0; i<zones.size(); i++) { options.add(new SelectOption(zones.get(i), zones.get(i))); } return options; } public String[] getZones() { return zones; } public void setZones(String[] zones) { this.zones = zones; } public List<String> getZoneR() { List<String> myZones = new List<String>(); Schema.DescribeFieldResult zoneField = Contact.ZoneR__c.getDescribe(); List<Schema.PicklistEntry> zoneValues = zoneField.getPicklistValues(); for (Integer i=0; i<zoneValues.size(); i++) { myZones.add(zoneValues.get(i).getValue()); } return myZones; } }





 Why have you used an array of String to store a single value?

 you could have used a String variable.


Try this, if it works..


public String getZones() {
return zones;

public void setZones(String zones) {
this.zones = zones;

Thanks for the suggestion but it didn't work either. I decided to just go with inputText and have some validation around which values should be allowed to be entered (i.e. the picklist values). It's not ideal, but is the only way I got it to work.


Thanks for all your inputs.


Use it in this way


function setHiddenvalue()


         objHi.value =  objSelec.value;



<apex:selectList id="mySelect" />.....

<apex:inputHidden id="selectedValue" value="{!selectedValue}"/>//set this as a property in controller


var objSelect = document.getElementById('{!$Component.mySelect}') ;

var objHid =  document.getElementById('{!$Component.selectedValue}') ;


<apex:commnadButton onclick="setHiddenvalue()" />


You will get the selected value in the controllers property selectedValue. cheers....

Message Edited by Srinivas_V2 on 04-03-2009 01:34 AM

Use Standard controller wid extension u will directly get the selectd value in controller..


Do like this :


I have picklist called Skill_Category__c


<apex:page standardController="Skill__c" id="thePage" extensions="SkillsController">


 <apex:inputField value="{!Skill__c.Skill_Category__c}" >




In Controller :


public class SkillsController {
private final ApexPages.StandardController controller;
private Skill__c cntr;

public SkillsController(ApexPages.StandardController controller) {

this.controller = controller;
cntr= (Skill__c )controller.getRecord();

public PageReference Save()
Skill__c newSkill = new Skill__c();
newSkill.Skill_Category__c = cntr.Skill_Category__c;

insert newSkill;
return null;