将多个单选按钮值从视觉力页面传递到控制器

Passing Multiple Radio buttons value from Visual force page to controller

我在视觉力页面上使用页面块 table 并且每一行都有单选按钮,我想将每个选定单选按钮的值从视觉力页面上的每一行传递到顶点控制器 class.

现在我只得到最后一行的一个值,我需要所选单选按钮的每个值。

视觉力页面

    <apex:pageBlockTable value="{!SurveyQuestions}" var="sq">
        <apex:column value="{!sq.Id}"/>
        <apex:column value="{!sq.Name}"/>
        <apex:column value="{!sq.QuestionID__r.Question__c}"/>
        <apex:column value="{!sq.QuestionID__r.Id}" headerValue="Question ID"/>
        <apex:column headerValue="HiddenQuestion Id">
         <apex:inputHidden value="{!sq.QuestionID__r.Id}" id="HdnQuestionID"/> 
        </apex:column>  
        <apex:column headerValue="Question options">
            <apex:selectRadio value="{!selectedAnswer}">
              <apex:selectOptions value="{!items}"/>
            </apex:selectRadio> 
        </apex:column>   
    </apex:pageBlockTable>

A​​pex 控制器Class

public with sharing class GbPracticeController {

public tblSurvey__c SurveyObj {get;set;}

public string SelectedAnswer                 {get;set;}
public list<tblSurveyQuestion__c> SurveyQuestions {get;set;}

public GbPracticeController(ApexPages.StandardController controller) {
        SurveyObj = new tblSurvey__c(); 

}


public PageReference SaveSurvey() {  
    insert SurveyObj;
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Survey Submitted Successfully'));

    return null;
}

//Listing Survey Related Questions
public ApexPages.StandardSetController setSurveyQuestions {
    get {
        if(setSurveyQuestions == null) {
            setSurveyQuestions = new ApexPages.StandardSetController(Database.getQueryLocator(
                [SELECT Id,Name,fo__QuestionID__c,fo__SurveyID__c, fo__QuestionID__r.Id,fo__QuestionID__r.Name,fo__QuestionID__r.fo__Question__c, fo__QuestionID__r.fo__SelectedAnswer__c  FROM fo__tblSurveyQuestion__c WHERE fo__SurveyID__c = 'a0R28000000FfXaEAK']));
        }
        return setSurveyQuestions;
    }
    set;
}
// Initialize getSurveyQuestions and return a list of records
public List<tblSurveyQuestion__c> getSurveyQuestions() {
    return (List<tblSurveyQuestion__c>) setSurveyQuestions.getRecords();
}

public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>(); 
    options.add(new SelectOption('yes','Yes')); 
    options.add(new SelectOption('no','No')); 
    options.add(new SelectOption('Maybe','maybe')); 
    return options; 
}

public PageReference SubmitResult() {

// Multiple Checkbox value Logic Goes Here
for (SurveyQuestionsType sqItem : SurveyQuestions) {
        if(sqItem.SelectedAnswer__c == 'yes') {

               //Inserting The Survey Result        
               tblSurveyResult__c SurveyResult = new tblSurveyResult__c(
                      Name = sqItem.SelectedAnswer__c,
                      Answer__c = 1
                     );
               insert SurveyResult;


            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Survey Submitted Successfully'));

        } else if (sqItem.SelectedAnswer__c == 'no'){

            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,sqItem.SelectedAnswer__c));

        } else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select Atleast one value'));
        }
    }
    return null;
}
// qwerty
}

我建议您添加一个新的自定义字段,例如 SelectedAnswer,更改

apex:selectRadio value="{!selectedAnswer}">

apex:selectRadio value="{!sq.SelectedAnswer__c}">

控制器更改为

public PageReference SubmitSurvey() {

// Multiple Checkbox value Logic Goes Here
foreach (SurveyQuestionsType sqItem : getSurveyQuestions()) {
if(sqItem.SelectedAnswer__c == 'yes'){

           //Inserting The Survey Result

           tblSurveyResult__c SurveyResult = new tblSurveyResult__c(
                  Name = sqItem.SelectedAnswer__c,
                  Answer__c = 1
                 );
           insert SurveyResult;


    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Survey Submitted Successfully'));

} else if (sqItem.SelectedAnswer__c == 'no'){

    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,sqItem.SelectedAnswer__c));

} else {
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select Atleast one value'));
}
}
return null;
}

或者,您可以使用新的 SelectedAnswer 字段创建此对象的 DTO 并使用 DTO,而不是仅向您的自定义对象添加一个字段。

此外,在错误输出期间,我会添加错误回答的指示。

回答我自己的问题,可能对其他人有帮助

VF 页面

<apex:pageBlockTable value="{!SurveyQuestions}" var="sq">
        <apex:column value="{!sq.Id}"/>
        <apex:column value="{!sq.Name}"/>
        <apex:column value="{!sq.QuestionID__r.Question__c}"/>
        <apex:column value="{!sq.QuestionID__r.Id}" headerValue="Question ID"/>
        <apex:column headerValue="HiddenQuestion Id">
          <apex:inputHidden value="{!sq.QuestionID__r.Id}"/>
        </apex:column>  
        <apex:column headerValue="Question options">
            <apex:selectRadio value="{!sq.QuestionID__r.SelectedAnswer__c}">
              <apex:selectOptions value="{!items}"/>
            </apex:selectRadio> 
        </apex:column>  
    </apex:pageBlockTable>

    <apex:commandButton value="Submit Survey Result" action="{!SubmitSurvey}" rerender="StartSurveyForm"/>


顶点Class代码

public PageReference SubmitSurvey() {

// Multiple Checkbox value Logic Goes Here

for (tblSurveyQuestion__c sqItem : getSurveyQuestions()) {
    if(sqItem.QuestionID__r.SelectedAnswer__c == 'yes'){

               //Inserting The Survey Result

               tblSurveyResult__c SurveyResult = new tblSurveyResult__c(
                      Name = sqItem.QuestionID__r.SelectedAnswer__c,
                      Answer__c = decimal.valueof('1'),
                      QuestionID__c = sqItem.QuestionID__r.Id,
                      SurveyID__c= surveyId
                     );
               insert SurveyResult;

        }  else{

              ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Select Atleast one value'));  
        }
    }
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,’Survey Result Successfully Submitted'));  
    return null;}