+ Start a Discussion
Justin DenningJustin Denning 

Rerender clearing values?

I have a dropdown list of regions that seems to be clearing all values in my controller, and I don't know what I'm doing wrong.  Below is the controller and VF page along with 2 pictures- one showing values loading on the initial page load, and showing no values when the Region dropdown is changed.  

Any help is greatly appreciated.

Page at Load
Page after changing the dropdown- RegionId and SelectOptions are cleared
After changing dropdown.
public with sharing class RegionController {

   public  static Id RegionId {get;set;}
   public  static Map<Id, Region__c> Regions {get;set;}
   public  static List<SelectOption> regionOptions {get;set;}


public RegionController
{
     //Default Id for testing
     RegionId = 'a0KA0000001IUO0MAO';

     //Get Map of regions
     Regions =  new Map<Id, Region__c>([SELECT ID, Name FROM Region__c]);

     //Set Region Options
     SelectOption sO;
     regionOptions = new List<SelectOption>();
     for (Id i : Regions.keySet())
     {
         sO = new SelectOption(i, Regions.get(i).Name);
         regionOptions.add(sO);
     } 


}

public PageReference DoNothing() 
    {
        return null;
    }

}
 
<apex:page showHeader="true" controller="RegionController" sidebar="true">

<apex:form>
  <apex:selectList size="1"  value="{!RegionId}" >
              <apex:actionSupport event="onchange" action="{!DoNothing}"  rerender="RegionPanel"    />
              <apex:selectOptions value="{!regionOptions}"/>
  </apex:selectList>
</apex:form>
        
  <apex:outputPanel id="RegionPanel" >
        Region {!RegionId}<br/>
        Options <br/>
         <apex:repeat value="{!RegionOptions}" var="regions">
              {!regions}<br/>
         </apex:repeat>
  </apex:outputPanel>
</apex:page>

 
Best Answer chosen by Justin Denning
Steven NsubugaSteven Nsubuga
public with sharing class RegionController {

   public  static Id RegionId {get;set;}
   public  static Map<Id, Region__c> Regions {get;set;}
   public  static List<SelectOption> regionOptions {get;set;}

    public RegionController() {
    
         //Default Id for testing
         RegionId = 'a0KA0000001IUO0MAO';
    }
    
    public List <SelectOption> getRegionData() {
    //Get Map of regions
         Regions =  new Map<Id, Region__c>([SELECT ID, Name FROM Region__c]);
    
         //Set Region Options
         SelectOption sO;
         regionOptions = new List<SelectOption>();
         for (Id i : Regions.keySet())
         {
             sO = new SelectOption(i, Regions.get(i).Name);
             regionOptions.add(sO);
         }
         return regionOptions;
    }
    
    public PageReference DoNothing() 
        {
            return null;
        }
    
}


 
<apex:page showHeader="true" controller="RegionController" sidebar="true">

<apex:form >
  <apex:selectList size="1"  value="{!RegionId}" >
              <apex:actionSupport event="onchange" action="{!DoNothing}"  rerender="RegionPanel"    />
              <apex:selectOptions value="{!RegionData}"/>
  </apex:selectList>
</apex:form>
        
  <apex:outputPanel id="RegionPanel" >
        Region {!RegionId}<br/>
        Options <br/>
         <apex:repeat value="{!RegionOptions}" var="regions">
              {!regions}<br/>
         </apex:repeat>
  </apex:outputPanel>
</apex:page>


 

All Answers

Steven NsubugaSteven Nsubuga
public with sharing class RegionController {

   public  static Id RegionId {get;set;}
   public  static Map<Id, Region__c> Regions {get;set;}
   public  static List<SelectOption> regionOptions {get;set;}

    public RegionController() {
    
         //Default Id for testing
         RegionId = 'a0KA0000001IUO0MAO';
    }
    
    public List <SelectOption> getRegionData() {
    //Get Map of regions
         Regions =  new Map<Id, Region__c>([SELECT ID, Name FROM Region__c]);
    
         //Set Region Options
         SelectOption sO;
         regionOptions = new List<SelectOption>();
         for (Id i : Regions.keySet())
         {
             sO = new SelectOption(i, Regions.get(i).Name);
             regionOptions.add(sO);
         }
         return regionOptions;
    }
    
    public PageReference DoNothing() 
        {
            return null;
        }
    
}


 
<apex:page showHeader="true" controller="RegionController" sidebar="true">

<apex:form >
  <apex:selectList size="1"  value="{!RegionId}" >
              <apex:actionSupport event="onchange" action="{!DoNothing}"  rerender="RegionPanel"    />
              <apex:selectOptions value="{!RegionData}"/>
  </apex:selectList>
</apex:form>
        
  <apex:outputPanel id="RegionPanel" >
        Region {!RegionId}<br/>
        Options <br/>
         <apex:repeat value="{!RegionOptions}" var="regions">
              {!regions}<br/>
         </apex:repeat>
  </apex:outputPanel>
</apex:page>


 
This was selected as the best answer
Justin DenningJustin Denning
Thanks so much Steven- forgot all about the getter for options.