function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DeepikareddyDeepikareddy 

Add functionality for the Accountobject

I had created a prgrame in such a way that,if i click on the add, the data entered in the input filed will bind to the list.. 
note: the code is working fine, but the issue is for the first record is not getting added, may i know why ?? onclicking on next time the record is getting ading.
 
apex code as follows:

 
public class accountexample {
 
  
  
  public account ac{get;set;}

   public list<account> lstacc{get;set;}
   
    public  accountexample(){
    lstacc= new list<account>();
    ac = new account();
    //addtolist();
     }
     
      public void addtolist(){
       ac = new account();
       lstacc.add(ac);
       system.debug(lstacc);
      }
     
      
  
}

Visuaforce page:
 
<apex:page controller="accountexample">
  <apex:form >
   <apex:pageblock >
     <apex:pageblockSection >
      
       <apex:inputField value="{!ac.name}"/>
       
       <apex:inputField value="{!ac.phone}"/>
       
       <apex:inputField value="{!ac.rating}"/>
       
     </apex:pageblockSection>
     
      <apex:pageblockButtons >
        <apex:commandButton value="add" action="{!addtolist}" reRender="t" />
      
      </apex:pageblockButtons>
   </apex:pageblock>
   
   
    <apex:pageblock >
    <apex:pageBlockTable value="{!lstacc}" var="a" id="t">
        <apex:column headerValue="Name">{!a.name}</apex:column>
        <apex:column headerValue="Phone">{!a.phone}</apex:column>
         <apex:column headerValue="Rating">{!a.rating}</apex:column>
      </apex:pageBlockTable>
    
    </apex:pageblock>
   </apex:form>
</apex:page>

note: for the 1st time the record is not getting added,but for the second and thrid and soon on the data is getting added.

Thanks
Deepika,