Why City values are not showing in DropDown?

22 Views Asked by At

VF page

<apex:page controller="EmployeeRegistrationController" showHeader="false" sidebar="false" standardStylesheets="false">
    <html>
        <head>      
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"/>
        </head>
        <body>
            <div class="container mt-5">
                <h2>Employee Registration</h2>
                <apex:form >
                    <div class="form-group">
                        <label for="firstName">First Name*</label>
                        <apex:inputText id="firstName" styleClass="form-control" required="true" value="{!empRegistration.Name}"/>
                    </div>
                    <div class="form-group">
                        <label for="lastName">Last Name*</label>
                        <apex:inputText id="lastName" styleClass="form-control" required="true" value="{!empRegistration.Last_Name__c}"/>
                    </div>
                    <div class="form-group">
                        <label for="state">State</label>
                        <apex:selectList id="state" styleClass="form-control" value="{!empRegistration.State__c}" size="1">
                            <apex:selectOptions value="{!stateOptions}" />
                            <apex:actionSupport event="onchange" action="{!loadCityOptions}" reRender="city" />
                        </apex:selectList>
                    </div>
                    <div class="form-group">
                        <label for="city">City</label>
                        <apex:selectList id="city" styleClass="form-control" value="{!empRegistration.City__c}" size="1" rendered="{!(ISBLANK(empRegistration.State__c))}">
                            <apex:selectOptions value="{!cityOptions}" />
                        </apex:selectList>
                    </div>
                    <div class="form-group">
                        <apex:commandButton styleClass="btn btn-primary" value="Register" action="{!saveRecord}" />
                    </div>
                </apex:form>
            </div>
        </body>
    </html>
</apex:page>

Apex Controller

public with sharing class EmployeeRegistrationController {

public Employee__c empRegistration { get; set; }
public List<SelectOption> stateOptions { get; set; }
public List<SelectOption> cityOptions { get; set; }

public EmployeeRegistrationController() {
    empRegistration = new Employee__c();
    loadStateOptions();
    cityOptions = new List<SelectOption>();
    
}
public void loadStateOptions() {
    stateOptions = new List<SelectOption>();
    stateOptions.add(new SelectOption('Maharashtra', 'MH'));
    stateOptions.add(new SelectOption('Madhya Pradesh', 'MP'));
    stateOptions.add(new SelectOption('Uttar Pradesh', 'UP'));
    System.debug('stateOptions'+stateOptions);
}

public void loadCityOptions() {
    if (empRegistration.State__c == 'Maharashtra') {
        cityOptions.add(new SelectOption('Nagpur', 'NA'));           
    } else if (empRegistration.State__c == 'Madhya Pradesh') {
        cityOptions.add(new SelectOption('Indore', 'IN'));
    } else if (empRegistration.State__c == 'Uttar Pradesh') {
        cityOptions.add(new SelectOption('Noida', 'No'));
    }
    System.debug('cityOptions---->'+cityOptions);
    
}
public PageReference saveRecord() {
    try {
        System.debug('empRegistration---->'+empRegistration);
        insert empRegistration;  
        System.debug('Emp Id:'+empRegistration.Id);
        PageReference successPage = Page.EmployeeRegistrationPage;
        successPage.setRedirect(true);
        return successPage;  
    } catch (Exception e) {
        System.debug('Error:'+e);
        ApexPages.addMessages(e);
        return null;
    }
}

}

I want to make a VF Page which provide me capability to create employee record by using VF Page and I have four fields on screen Name,LastName,State and City and City should be dependent on state but city showing me nothing

0

There are 0 best solutions below