assignTo attribute not setting value to contoller variable

I am trying to assign value to a controller variable using assignTo attribute of apex:param component as shown below

<apex:page controller="PageRefController">
<apex:form >
    <apeX:pageBlock title="Contacts">
        <apex:dataTable value="{!contacts}" var="con" cellpadding="4" border="1">
            <apex:column >
                <apex:facet name="header">Name</apex:facet>
                <apex:commandLink action="{!redirPage}">
                    <apex:param assignTo="{!name}" value="{!con.FirstName}" />
                    {!con.firstname}
                </apex:commandLink>
            </apex:column>
            <apex:column >
                <apeX:facet name="header">Phone</apex:facet>
                {!con.phone}
            </apex:column>
        </apex:dataTable>
    </apeX:pageBlock>
</apex:form>

public class PageRefController {
    public String name{get;set;}
    public List<Contact> getContacts(){
        return [Select firstname,phone from Contact limit 5];
    }
    public PageReference redirPage(){
        PageReference pg = new PageReference('/apex/TestPageRefEx?'+'name='+name);
        return pg;
    }
}

But my controller variable name is set to null with the above code snippet.However,if I use name attribute inside apex:param

<apex:param assignTo="{!name}" value="{!con.FirstName}" name="abc"/>

the value is set properly. Can anyone please explain this behavior ?

Answer

It is sent to salesforce as a name,value pair. If it doesn’t have a name, it isn’t available.
It is not required because <apex:param> has other uses to like in <apex:outputText>. The value is therefore the only required field.

Attribution
Source : Link , Question Author : codebandit , Answer Author : Marcel Meijer

Leave a Comment