You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visualforce页面单选框选中值无法保存到自定义对象的问题排查

问题描述

我尝试将Visualforce页面中选中的单选框值(比如“Excellent”)保存到自定义对象RB_Rating__c中,但点击提交按钮后值没有成功保存。以下是相关代码,请帮忙排查错误:

Visualforce代码

<apex:page  controller="RadioBtn_table_Apex" showHeader="false" applyHtmlTag="true" applyBodyTag="false">
<style>
    .fswrapper{
            width:800px;
            margin:0 auto;
            border:3CCF4E 30px;
        }
    
    .headerTitle
        {
            font-family:arial,sans-serif;
            font-size:16px;
            font-weight:600;
            color: #fff!important;
            text-align:left;
            text-transform:UPPERCASE;
            background:#4e75a3;
            Padding:10px;
            display:block;
            margin:0;
            
        }
    
    input[type=radio] {margin-left: 40px;}
    .radioClass {margin-left: 5%;}
    
    .big {line-height: 2.8;}

</style>
    <apex:form styleClass="fswrapper" id="frm">
        <head>
            <apex:slds />
        </head>
        <center>
            <img src="{!$Resource.ForceDotCom}"/>           
        </center>
        <apex:pageBlock >
            <h1 class="headerTitle"> Application Feedback </h1>
                 <apex:pageBlockButtons location="bottom">
                      <apex:commandButton value="Submit" action="{!savefeedback}" reRender="frm"/>
                      <apex:commandButton value="Exit" action="{!feedbackexit}" reRender="frm"/>
                 </apex:pageBlockButtons>

            <apex:actionRegion >

                <Table width="100%" border="0" cellpadding="0" cellspacing="0" columns="0" > 
                    <tr >
                        <td colspan="5" class="fieldLabel">                     
                            <p style="font-size: 20px;">
                               <b> How likely are you with our Application </b>
                            </p>      
                            
                            <!--<apex:selectRadio id="selectRb" value="{!selectedAnswer}" immediate="true"> -->
                            <apex:selectRadio id="selectRb" value="{!ratobj.Application_Rating__c}">
                                <apex:selectOption value="{!items}"/>                                 
                            </apex:selectRadio>
                                
                        </td>
                     </tr> 
                 </Table>                           
           </apex:actionRegion> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex代码

public  class RadioBtn_table_Apex 
{
    
    public RB_Rating__c ratobj{get;set;} 
    public string SelectedAnswer {get;set;}
    
    public RadioBtn_table_Apex()
    {
      ratobj = new RB_Rating__c();        
    }
    
    public pagereference savefeedback()
    {
       
        insert ratobj;
       
       return Auth.SessionManagement.FinishLoginflow('/'+ratobj.Id);
    }
    
    public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>(); 
    options.add(new SelectOption('Excellent','Excellent')); 
    options.add(new SelectOption('Great','Great')); 
    options.add(new SelectOption('Good','Good')); 
    options.add(new SelectOption('Medium','Medium'));
    options.add(new SelectOption('Poor','Poor'));
    options.add(new SelectOption('Very Bad','Very Bad'));
    return options; 
}
    
    public pagereference feedbackexit()
    {
        
       return Auth.SessionManagement.FinishLoginflow('/');
    }

}
错误排查与修复

核心错误点

Visualforce页面中单选框的选项绑定标签使用错误:

  • 你使用了<apex:selectOption>(单数)来绑定getItems()返回的选项列表,但这个标签仅用于定义单个选项。绑定列表类型的选项必须使用<apex:selectOptions>(复数)标签。
  • 这个错误导致选中的单选框值无法正确绑定到ratobj.Application_Rating__c字段,最终插入到数据库时该字段为空。

修复步骤

修改Visualforce中的单选框部分代码:

<!-- 错误代码 -->
<apex:selectRadio id="selectRb" value="{!ratobj.Application_Rating__c}">
    <apex:selectOption value="{!items}"/>                                 
</apex:selectRadio>

<!-- 修复后代码 -->
<apex:selectRadio id="selectRb" value="{!ratobj.Application_Rating__c}">
    <apex:selectOptions value="{!items}"/>                                 
</apex:selectRadio>

额外检查项

  1. 确认自定义对象RB_Rating__c的Application_Rating__c字段类型:
    • 如果是文本类型:确保字段长度足够容纳选项值(比如“Very Bad”长度为8,字段长度至少设置为10以上)。
    • 如果是选择列表类型:确保所有选项值(Excellent、Great、Good等)都已在字段的选项配置中定义,否则值无法保存。
  2. 检查apex:actionRegion的范围:当前范围包含了单选框,提交时能正确提交该区域的表单数据,这部分没问题。

内容的提问来源于stack exchange,提问作者Tara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 03:31:03