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
Harshada Kadu 9Harshada Kadu 9 

fetching selected radio option value in apex class

Hello,
I have custom object Questions__c and Questionoption__c. they have master detail relationship. each question will have 6 options.
Now I want to display question and its options with radio buttons.
i am able to display it in visualforce page.
There are 4 questions with 6 options each.
so for each option selected a record should be created.
The selected value and its question I want to store in custom object.
I am unable to get selected value and its question from vf to apex class.

Following is my code:
VisualForce page:-
<apex:page id="thePage" showHeader="false" docType="HTML-5.0"  sidebar="false"
          standardController="Case" extensions="SurveyController_ex" standardStylesheets="false">   
      <apex:form >
    <html>
       
           
<div style="text-align: center; width: auto; margin: auto; background-color:#313131;padding-top:0.6%;padding-bottom:0.6%;">
<table style="text-align: center; margin: auto; width: 99%;" border="0" cellspacing="0" cellpadding="0" bgcolor="#151515">
<tbody>
<tr>
<td align="center" style="padding-top:0.3%"><img src="{!image}" alt="" height="45" border="0" /></td>
</tr>
<tr>
<td style="font-family: Arial,Helvetica,sans-serif; font-size: 12px; text-align: left;" bgcolor="#FFFFFF">
<div>
<div style="border-bottom: solid 1px #ddd; margin: 0px 5px 10px 5px;">&nbsp;</div>
<div id="reply" style="margin-left: 10px;">
      <br/>
    
     
         
       
          
         <!---input type="radio" name="option" checked="" value="{!op.Option__c}"/>
         <apex:outputPanel>
         {!op.Option__c}
        </apex:outputPanel-->
            <!---apex:selectRadio>
             <apex:selectOption itemLabel="{!q.sop}" itemValue="{!q.sop}" itemDisabled="false" value="{!q.sop}">
                </apex:selectOption>
             </apex:selectRadio--->
    
    
    <apex:repeat value="{!QU}" var="q" id="questionContainer">
        
       <apex:outputPanel >
             <div >
                 <apex:outputText>{!q.Question__c}</apex:outputText>
          </div>    
          
           <apex:selectRadio layout="lineDirection" value="{!getselectedvalue}">
                   <apex:selectOptions value="{!seloption[q.Id]}">
                     
                  </apex:selectOptions>
           </apex:selectRadio>
           
      </apex:outputPanel>
    </apex:repeat>
    
</div>
<div style="border-bottom: solid 1px #ddd; margin: 0px 5px 10px 5px;">&nbsp;</div>
    <apex:commandButton title="Submit" value="Submit" action="{!Submit}" />
<div style="color: #777; font-size: 10px; margin: 0px 0px 0px 10px;"><span style="background-color: #d8f4fa;"></span></div>
<div style="border-bottom: solid 1px #ddd; margin: -1px 5px 10px 5px;">&nbsp;</div>
</div>
</td>
</tr>
<tr style="font-family: Arial,Helvetica,sans-serif; font-size: 9px; text-align: center; color: #ffffff;" height="20">
<td style="padding-top:4px;padding-bottom:2px;"><apex:outputLabel value="{!footer}"></apex:outputLabel> &copy;<apex:outputText value="{!Case.CurrentYear__c}"></apex:outputText></td>
</tr>
</tbody>
</table>
</div>
    </html>
    </apex:form>
</apex:page>
-------------------------------------------------------------------------------------------------------------------------------------------------------------
SurveyController(paex Class)

 public String Footer{get;private set;}
    Case c;
    List<QuestionOption__c> quesoption{get;private set;}    
    public List<Questions__c> questionlist {get;set;}
    public List<QuestionOption__c> quesoplist {get;set;}
    public Map<Id, List<QuestionOption__c>> quesidlistmap { get; set; }
    public List<QuestionWrapper> queswrapperlist {get;set;}
    Set<Id> qId = new Set<Id>();
    Map<Id,Questions__c> qmap = new Map<Id,Questions__c>();
    public List<SelectOption> seloptionlist {get;set;}
    public Map<Id,List<SelectOption>> seloption {get;set;}
     public List<Questions__c> quenop{get;set;}//testing
    
    public List<String> getselectedvalue{get;set;}
    public List<String> getquestion{get;set;}
       
    public SurveyController_ex(ApexPages.StandardController controller){
        Footer = '';
        
        
    }
    
    public String getImage()
    {
        String Brand='NetBet.com';
        String Logo;
        
        if(Brand=='NetBet.com')
        {
            Logo='https://img.netbet.co.uk/poker/marketing_images/NetBet_logo.png';
            Footer='NetBet';
        }
        
        else if(Brand=='NetBet.it')
        {
            Logo='https://img.netbet.co.uk/poker/marketing_images/NetBet_logo.png';
        }
        else if(Brand=='NetBet.fr')
        {
            Logo='https://img.netbet.co.uk/poker/marketing_images/NetBet_logo.png';
        }
        else if(Brand=='777.be')
        {
            Logo='https://css.casino777.be/desktop/Themes/casino777v5/images/layout/logos/navi_logo_4.png';
        }
        else if(Brand=='Betfirst.be')
        {
            Logo='https://bfscripts.dhnet.be/themes/sbtech/images/logo_full_en.png';
        }
        else if(Brand=='Casino777.es')
        {
            Logo='https://www.casino777.es/themes/casino770/css/img/fd_logo.png';
        }
        return Logo;
        
    }
    
    public class QuestionWrapper{
        public  Questions__c question {get; set;}
        public List<QuestionOption__c> optionlist {get;set;}
        //  public List<SelectOption> sop {get;set;}
        public QuestionWrapper(Questions__c question,List<QuestionOption__c> optionlist){
            this.question = question;
            this.optionlist = optionlist;
            //  this.sop = sop;
            //[select Id,Option__c,QuestionId__c ,Name FROM QuestionOption__c where QuestionId__c = :q.Id];
            
        }
    }
    
 
       
    
    public List<QuestionWrapper> getList()
    {
        questionlist = [SELECT Id, Name, Question__c, Language__c FROM Questions__c];
        quesidlistmap = new Map<Id, List<QuestionOption__c>> ();
        queswrapperlist = new List<QuestionWrapper>();
        
        
        for(Questions__c ques :questionlist ){
            qId.add(ques.Id);
            qmap.put(ques.Id,ques);
            
        }
        quesoplist = [SELECT Id,Option__c,Name,QuestionId__c FROM QuestionOption__c where QuestionId__c =:qid order by Name];
        
        //   for(Questions__c ques :questionlist ){
        
        for(QuestionOption__c oplist : quesoplist){
            if(!quesidlistmap.containsKey(oplist.QuestionId__c)){
                quesidlistmap.put(oplist.QuestionId__c, new List<QuestionOption__c>());
            }
            // newoption.add(new SelectOption(oplist.Id,oplist.Option__c));
            quesidlistmap.get(oplist.QuestionId__c).add(oplist);
        }
        //  seloption.put(ques.Id,newoption );
        //   }
        for(Id opid : quesidlistmap.keySet()){
            queswrapperlist.add(new QuestionWrapper(qmap.get(opid),quesidlistmap.get(opid)));
        }
    
        
        return queswrapperlist;
        
    }
 
    public list<Questions__c> getQU(){
        
        quenop = [SELECT Id, Name, Question__c, Language__c,(SELECT Id,Option__c,Name,QuestionId__c FROM QuestionOptions__r order by Name) FROM Questions__c order by Name];
        seloption = new Map<Id,List<SelectOption>>();
        for(Questions__c q : quenop){
            List<SelectOption> l = new List<SelectOption>();
            for(QuestionOption__c qops : q.QuestionOptions__r){
                 l.add(new SelectOption(string.valueof(qops.id), qops.Option__c));
                seloption.put(q.Id,l );
                System.debug('list of options'+ l);
                System.debug('map of options'+ seloption);
               

            }
            
        }
      
        System.debug('quenop: ' +quenop);
        return quenop;

        
    }
    public PageReference Submit(){
       
        List<Response__c> resplist = new List<Response__c>();
        for(String s : getselectedvalue){
             System.debug('getselectedvalue :'+ getselectedvalue);
            Response__c resp = new Response__c();
            
            resp.OptionId__c = s;
            resplist.add(resp);
             System.debug('Response :'+ resp);
            System.debug('Response list :'+ resplist);
        }
        insert resplist;
      //  resp.OptionId__c = getselectedvalue;
       // resp.QuestionId__c =getquestion;
        // insert resp;
       
       
        return null;
    }

Thanks & Regards,
Harshada