Salesforce 未知 属性 'String.Name'

Salesforce Unknown property 'String.Name'

我想列出表中的调查结果,但出现错误

Unknown property 'String.Name'

我需要在 VisualForce 页面上显示结果。

VF 页面

<apex:page showHeader="false" sidebar="false"  controller="SurveyResultController">
<apex:form>
   <apex:pageBlock title="Submited Result">
        <apex:pageBlockTable value="{!SurveyResult}" var="sr">
            <apex:column value="{!sr.Name}"/>            
        </apex:pageBlockTable>
   </apex:pageBlock> 
</apex:form>    

Apex 控制器

public with sharing class SurveyResultController {
    public String surveyId {
        get;
        set{
            this.surveyId = value;
        }
    }
    public Integer SurveySessionID {
        get;
        set{
            this.SurveySessionID = value;
        }
    }
    public String ssessionid {
        get;
        set;
    }
    public SurveyResultController() {
        surveyId = Apexpages.currentPage().getParameters().get('id');
        ssessionid = Apexpages.currentPage().getParameters().get('ssessionid');
    }
    public List<String> getSurveyResult() {
        List<tblSurveyResult__c> qr = [SELECT Name,
                                              QuestionID__c,
                                              SurveyID__c,
                                              Answer__c,
                                              QuestionID__r.Id,
                                              QuestionID__r.Name,
                                              QuestionID__r.Question__c,
                                              QuestionID__r.SelectedAnswer__c
                                        FROM tblSurveyResult__c
                                        WHERE SurveyID__c = :surveyId
                                          AND SurveySessionID__c = :SurveySessionID];
        List<String> resp = new List<String>();
        for (tblSurveyResult__c r : qr) {
            resp.add(r.Name);
        }

        return resp;
    }
}

在下一行

<apex:column value="{!sr.Name}"/>

您尝试从字符串实体中读取 Name

在控制器中你准备了一个 List<String> 并且 return 它到页面。

    List<String> resp = new List<String>();
    for (tblSurveyResult__c r : qr) {
        resp.add(r.Name);
    }

    return resp;

所以,你的选择:

  1. 页面使用如下格式

    <apex:column value="{!sr}"/>
    
  2. return tblSurveyResult__c 列表到页面

    public List<tblSurveyResult__c > getSurveyResult() {
        return [SELECT Name,
                       QuestionID__c,
                       SurveyID__c,
                       Answer__c,
                       QuestionID__r.Id,
                       QuestionID__r.Name,
                       QuestionID__r.Question__c,
                       QuestionID__r.SelectedAnswer__c
                FROM tblSurveyResult__c
                WHERE SurveyID__c = :surveyId
                  AND SurveySessionID__c :SurveySessionID];
    
    }
    

EDIT 由于您的意见,请使用第二个选项

Return tblSurveyResult__c 列表到页面

 public List<tblSurveyResult__c > getSurveyResult() {
        return [SELECT Name,
                       QuestionID__c,
                       SurveyID__c,
                       Answer__c,
                       QuestionID__r.Id,
                       QuestionID__r.Name,
                       QuestionID__r.Question__c,
                       QuestionID__r.SelectedAnswer__c
                FROM tblSurveyResult__c
                WHERE SurveyID__c = :surveyId
                  AND SurveySessionID__c :SurveySessionID];

  }




 <apex:page showHeader="false" sidebar="false"  controller="SurveyResultController">
<apex:form>
   <apex:pageBlock title="Submited Result">
        <apex:pageBlockTable value="{!SurveyResult}" var="sr">
            <apex:column value="{!sr.SurveyID__c}"/>
            <apex:column value="{!sr.Name}"/>
            <apex:column value="{!sr.QuestionID__r.Question__c}"/>
            <apex:column value="{!sr.QuestionID__r.SelectedAnswer__c}"/>
        </apex:pageBlockTable>
   </apex:pageBlock>
</apex:form>