+ Start a Discussion
tendonstrengthtendonstrength 

Can't get selectlist selection to bind with controller variable

Hello,

 

I have a visualforce page with a selectlist and I'm trying to get the user's selection to bind with a variable in the controller. When values are entered in the input fields of the page, they bind to the variables in the controller just fine, but selecting something in the list seems to have no effect on the variable that (I think) I have associated with it.

 

   Here is the code for the VF page:

 

 

<apex:page controller="ItemWizardController" tabStyle="Opportunity" >
<apex:pageMessages />

<apex:form >


<apex:pageBlock >

<apex:pageBlockSection columns="2" id="mainBlock">

<apex:pageBlockSectionItem >
<apex:outputLabel value="* Pick a Supplier: " for="supplierName"/>
<apex:inputField id="supplierName" value="{!newItem.Account__c}"/>
</apex:pageBlockSectionItem><br></br>

<!-- This is the list. I'm expecting the selection to bind with the variable itemType in the controller-->
<apex:pageBlockSectionItem id="itemtypelist">
<apex:outputLabel value="* Item Type: " for="itemType"/>
<apex:selectList id="itemType" size="1" value="{!itemType}">
<apex:selectOptions value="{!itemTypes}"/>
</apex:selectList>
</apex:pageBlockSectionItem><br></br>

<apex:pageBlockSectionItem >
<apex:outputLabel value="Logo Name: " for="logoName"/>
<apex:inputField id="logoName" value="{!newItem.Logo_Name__c}"/>
</apex:pageBlockSectionItem><br></br>

<apex:pageBlockSectionItem >
<apex:outputLabel value="Logo Placement: " for="logoPlacement"/>
<apex:inputField id="logoPlacement" value="{!newItem.Logo_Placement__c}"/>
</apex:pageBlockSectionItem>

</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton action="{!quit}" value="Cancel" styleClass="btn"/>
<apex:commandButton action="{!goToPage2}" value="Next" styleClass="btn"/>
</apex:pageBlockButtons><br></br>
</apex:pageBlock>

</apex:form>

</apex:page>

 And here is the code for the controller:

 

 

public class ItemWizardController {
	
	
	private Item_WIP_MWA__c newItem;
	private String itemType;
	private List<SelectOption> itemTypes;
	private String oppId;

	
    public ItemWizardController() {
    	Item_WIP_MWA__c item = new Item_WIP_MWA__c();
		this.newItem = item;
		this.oppId = ApexPages.currentPage().getParameters().get('oppId');
    }        

	
	public Item_WIP_MWA__c getNewItem(){
		return this.newItem;
	}
	

	public String getItemType(){
		return this.itemType;
	}
	
	public void setItemType(String itemType){
		this.itemType = itemType;
	}

    public List<SelectOption> getItemTypes(){
    	List<SelectOption> optionList = new List<SelectOption>();
    	optionList.add(new SelectOption('', '-None-'));
    	optionList.add(new SelectOption('', 'Apparel'));
    	optionList.add(new SelectOption('', 'Other'));
    	return optionList;
    }

	public String getOppId(){
		return this.oppId;
	}
	
	public void setOppId(String oppId){
		this.oppId = oppId;
	}
	
	
    public PageReference quit(){
    	PageReference oppPage = new PageReference('/' + oppId);
    	return oppPage;
    }
    
    public PageReference goToPage2(){  	 	
    	return Page.itemStep2;		
    }  
   
    

}

 

 

One thing to keep in mind is that the value in the select list doesn't bind to a field in an SObject. This is going to be a wizard interface of sorts and the value there is just used so that the controller knows what page it needs to go to next.

 

Thanks in advance.

 

Best Answer chosen by Admin (Salesforce Developers) 
tendonstrengthtendonstrength

I found the answer I was looking for in this thread:

 

http://boards.developerforce.com/t5/Visualforce-Development/The-simplest-selectlist-problem/m-p/125986/highlight/false#M12507

 

To summarize, most selectlist examples (and the code I posted above), show the apex SelectOption constructor being called like this:

 

 

new SelectOption(' ', 'Apparel');  //notice that the first parameter is being passed as an empty string

 The problem is that the first option actually needs to be set to the value that you want set when that option is selected like so:

 

 

new SelectOption('Apparel', 'Apparel');  //notice that the first parameter is being passed as an empty string

 

 

 

 

 

 

All Answers

Pradeep_NavatarPradeep_Navatar

Tryout this sample code to bind the picklist values with controller :

 

VF CODE:

       <apex:form>

           <apex:selectList value="{!ListProfile}"  size="1"   onchange="FetchData(this.options[this.selectedIndex].text);" >

                 <apex:selectOptions value="{!Pickvalues}"></apex:selectOptions>

            </apex:selectList>

 

                <apex:outputPanel></apex:outputPanel>

                <apex:actionFunction action="{!GivePortalAccess}" reRender="refreshdt ">

                                      <apex:param assignTo="{!ListProfile}" value=""/>

                </apex:actionFunction>

          </apex:form>

 

Controller code:

 

               Public String ListProfile{get;set;}

tendonstrengthtendonstrength

I found the answer I was looking for in this thread:

 

http://boards.developerforce.com/t5/Visualforce-Development/The-simplest-selectlist-problem/m-p/125986/highlight/false#M12507

 

To summarize, most selectlist examples (and the code I posted above), show the apex SelectOption constructor being called like this:

 

 

new SelectOption(' ', 'Apparel');  //notice that the first parameter is being passed as an empty string

 The problem is that the first option actually needs to be set to the value that you want set when that option is selected like so:

 

 

new SelectOption('Apparel', 'Apparel');  //notice that the first parameter is being passed as an empty string

 

 

 

 

 

 

This was selected as the best answer