Skip to main content
2 of 3
added 535 characters in body
Keith C
  • 137.3k
  • 29
  • 218
  • 458

You can build the SelectOption objects in your controller:

public Id selectedUserId {get; set;}

public List<SelectOption> getUserSelectOptions() {
    List<SelectOption> options = new List<SelectOption>();
    for (User u : [Select id, Name from User order by Name]) {
        options.add(new SelectOption(u.Id, u.Name));
    }
    return options;
}

and so the Visualforce becomes:

<apex:selectList value="{!selectedUserId}" size="1">
    <apex:selectOptions value="{!userSelectOption}"/>
</apex:selectList>

Note that the earlier version of this answer suggested using an apex:repeat to allow multiple apex:selectOption elements but as Carlos reports in his comment below Visualforce validates against that.

Keith C
  • 137.3k
  • 29
  • 218
  • 458