Search code examples
parameterscontrollervisualforcecommandbutton

Why am i not getting values from visualforce page on my controller?


when I try to get the selected values from picklist and the inputText value by clicking the command button, it's not really passing them to my controller.

VF:

<apex:page controller="FieldsAndTypesPicklists" >
<apex:form >

    <apex:inputText value="{!newDatasetName}" />
    <apex:outputlabel value="Product type: "/>   

    <apex:selectList value="{!selectedTypeProd}" size="1">
        <apex:selectOptions value="{!TypesProduct}"/>
    </apex:selectList>

    <apex:commandButton value="Add new values" action="{!SaveValues}"  />
</apex:form>
</apex:page>

Controller:

public with sharing class FieldsAndTypesPicklists {

    public String selectedTypeProd {get; set;}

    public String newDatasetName { get; set; }

    public void SaveValues() {
        System.debug('>>> InputText value: '+newDatasetName);
    }

     public Set<SelectOption> getTypesProduct(){

        System.debug('>>> Select Type value: '+selectedTypeProd);
        Set<SelectOption> typesProd = new Set<SelectOption>();
        List<Schema.PicklistEntry> picklistEntryList = OpportunityLineItem.TypeProduct__c.getDescribe().getPicklistValues();

        for(Schema.PicklistEntry plEntry : picklistEntryList){
            String typeProduct = string.ValueOf(plEntry.getValue());
            typesProd.add(new SelectOption(typeProduct, typeProduct));
        }

        return typesProd;
    }  

 }

I've made the corresponding debugs to see the inputText and the picklist selected value.

  1. selectedTypeProd is null
  2. it is not executing the method SaveValues()

If I take out the piece of code where is the selected list (on the VF) and the getTypesProduct() method in the controller it works fine for the inputText value. It seems like the other part is affecting the execution.


Solution

  • You can make TypesProduct as a set of strings. So it will works.