[SalesForce] How to display List values in VF Page

Apex Class:

public  class RelatedController 
{

    public Case selectedCase { get; set; }
    public List<VEH_Veh__c> selectedveh { get; set; }
    public RelatedController ()
    {

         System.debug('selectedCase ----------------------->>'+selectedCase );
    }

    public List<VEH_Veh__c> readUser()
    {
        selectedveh = [ Select Id,Model__C from VEH_Veh__c Where Id = :selectedCase.VIN__c ];
        System.debug('selectedveh----------------------->>'+selectedveh );
        return selectedveh ;
    } 
    private ApexPages.StandardController stdCtrl;

    public RelatedController(ApexPages.StandardController std)
    {
        stdCtrl=std;
    }
}

Apex Page:

<apex:page standardcontroller="Case"  extensions="RelatedController">
  <apex:form >
    <apex:pageMessages id="msgs"/>
    <apex:pageBlock title="Contact Create/Edit">

      <apex:actionRegion >
        <apex:pageBlockSection id="accinfo" title="Account Information">
          <apex:inputField value="{!selectedCase .VIN__c}">
            <apex:actionSupport event="onchange" action="{!readUser}" rerender="section" />
          </apex:inputField>
        <apex:outputPanel id="section">
        <apex:repeat value="{!selectedveh}" var="a">
          <apex:outputField value="{!a.Model__c}"/>
          </apex:repeat>
          </apex:outputPanel>
        </apex:pageBlockSection>
      </apex:actionRegion>

      <apex:pageBlockButtons >
        <apex:commandButton value="Cancel" action="{!cancel}"/>
        <apex:commandButton value="Save" action="{!save}"/>
      </apex:pageBlockButtons>

     </apex:pageBlock>
  </apex:form>
</apex:page>

Error:(Nothing is happening)
enter image description here

Best Answer

You have to use PageReference as a method return type when Apex action method is used. Also inside Constructor you have to either assign the Case (from URL parameter) or Create a new Case based on the scenario (edit or new Case creation).

Your code should somehow look like this. Please modify your code as below. Remember, this is not a compiled code.

VF Page

<apex:page standardController="Case" extensions="RelatedController" >
    <apex:form>
        <apex:actionRegion>
            <apex:inputField value="{!selectedCase.VIN__c}">
                <apex:actionSupport event="onchange" action="{!readUser}" reRender="section"  /> 
            </apex:inputField>
            <apex:outputPanel id ="section">
                <apex:repeat var="a" value="{!selectedveh}">
                    <apex:outputText value="{!a.Name}"/>
                </apex:repeat>
            </apex:outputPanel>
        </apex:actionRegion>
    </apex:form>
</apex:page>

Controller

public class RelatedController {
    public Case selectedCase {get;set;}
    public List<VEH_Veh__c> selectedveh {get;set;}
    public RelatedController(ApexPages.StandardController std){
        if(selectedCase==null){
            selectedCase = new Case();
        }
    }
    public PageReference readUser(){
        selectedveh = [SELECT Name FROM VEH_Veh__c WHERE Id = :selectedCase.VIN__c];
        System.debug('%%'+selectedveh+'%%'+selectedCase.VIN__c);
        return null;
    }
}
Related Topic