Visualforce“转换错误设置值” -- selectCheckboxes 的绑定问题
当我尝试在控制器扩展中保存记录时,收到“转换错误设置值”。该页面看起来符合我的预期,但是当我选择一个或多个复选框时,它会给出该错误。我没有看到这里的错误是什么。感谢您的帮助。
页面:
<apex:pageBlockSectionItem >
<apex:OutputLabel value="Assigned Areas of Coverage" for="books" />
<apex:selectCheckboxes value="{!selectedBooks}"
layout="pageDirection" id="books">
<apex:selectOptions value="{!options}" />
</apex:selectCheckboxes>
</apex:pageBlockSectionItem>
控制器:
public String[] selectedBooks {get; set;}
public List<SelectOption> options
{
get
{
List<SelectOption> result = new List<SelectOption>();
List<String> optionNames = bookNames(books);
optionNames.sort();
for(String n : optionNames){
if(!blacklist.contains(n)){
result.add(new SelectOption(n, n));
}
}
return result;
}
}
private List<Book__c> books
{
get
{ if (books == null){
books = [select Id, Name from Book__c];
}
return books;
}
set;
}
private List<String> bookNames(List<Coverage__c> coverage)
{
List<String> result = new List<String>();
for(Coverage__c c : coverage){
result.add(c.Book__r.Name);
}
return result;
}
private List<String> bookNames(List<Book__c> books)
{
List<String> result = new List<String>();
for(Book__c b : books){
result.add(b.Name);
}
return result;
}
private List<Id> bookIDs(List<String> bookNames)
{
List<Id> result = new List<Id>();
Set<String> bookNamesSet = new Set<String>(bookNames);
for(Book__c b : books){
if(bookNamesSet.contains(b.Name)){
result.add(b.Id);
}
}
return result;
}
I am getting a 'Conversion error setting value' when I try to save a record in my controller extension. The page looks the way I would expect, but when I select one or more checkboxes, it gives my that error. I'm not seeing what the error is here. Thanks for the help.
The page:
<apex:pageBlockSectionItem >
<apex:OutputLabel value="Assigned Areas of Coverage" for="books" />
<apex:selectCheckboxes value="{!selectedBooks}"
layout="pageDirection" id="books">
<apex:selectOptions value="{!options}" />
</apex:selectCheckboxes>
</apex:pageBlockSectionItem>
The controller:
public String[] selectedBooks {get; set;}
public List<SelectOption> options
{
get
{
List<SelectOption> result = new List<SelectOption>();
List<String> optionNames = bookNames(books);
optionNames.sort();
for(String n : optionNames){
if(!blacklist.contains(n)){
result.add(new SelectOption(n, n));
}
}
return result;
}
}
private List<Book__c> books
{
get
{ if (books == null){
books = [select Id, Name from Book__c];
}
return books;
}
set;
}
private List<String> bookNames(List<Coverage__c> coverage)
{
List<String> result = new List<String>();
for(Coverage__c c : coverage){
result.add(c.Book__r.Name);
}
return result;
}
private List<String> bookNames(List<Book__c> books)
{
List<String> result = new List<String>();
for(Book__c b : books){
result.add(b.Name);
}
return result;
}
private List<Id> bookIDs(List<String> bookNames)
{
List<Id> result = new List<Id>();
Set<String> bookNamesSet = new Set<String>(bookNames);
for(Book__c b : books){
if(bookNamesSet.contains(b.Name)){
result.add(b.Id);
}
}
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您必须发布更多代码,并且不清楚您到底希望实现什么:
您的实际“命令”功能是什么,我没有看到任何并且控制器中没有一个函数看起来像“public void fun()”或“public PageReference fun()” - 我特别指的是不接受输入参数的部分。
您的代码无法编译,我已将 Book__c 替换为 Account,将 Coverage 替换为 Contact,但例如中的“黑名单”变量
if(!blacklist.contains(n)){
未定义。我可以假设这与“结果”相同,但你知道......垃圾进,垃圾出。我们可能只需要您省略的部分;)另外 - 您能否使用标准 Salesforce 对象复制问题,以便更轻松地为我们进行测试?
“selectedBooks”已在页面中读取,但您从未设置值?即使是最简单的 selectedBooks = new String[]...未初始化的变量 a 是 在 Salesforce 留言板上讨论了类似问题。
只是一个随机的想法 - 对于 bookIDs() 函数,你可能会更好使用 Map;或 Map。如果需要,第一个甚至可以直接从 [SELECT Id, Name FROM Book] 实例化。您需要一组 ID - 之后使用 keySet() 方法。您想要一个书籍列表 - 使用 value()。
I think you have to post more code and it's not clear what exactly you wish to achieve:
What's your actual "command" function, I don't see any <apex:commandButton action="{!fun}" ... > and not a single function from the controller looks like "public void fun()" or "public PageReference fun()" - I especially mean the part about taking no input arguments.
Your code does not compile, I've replaced Book__c with Account and Coverage with Contact, but for example the "blacklist" variable in
if(!blacklist.contains(n)){
is undefined. I can assume that this is same as "result", but you know... garbage in, garbage out. We might need just the part you've omitted ;) Also - can you replicate the problem using standard Salesforce objects so it's easier to test for us?
"selectedBooks" is read in the page but you never set value? Not even to the simplest selectedBooks = new String[]... The uninitialized variable a is solution to similar problem discussed on Salesforce message board.
Just a random thought - for bookIDs() function you might be better using Map<Id, Book__c> or Map<Id, String>. The first one can even be instantiated directly from [SELECT Id, Name FROM Book] if you need it. You want set of IDs - use the keySet() method afterwards. You want a list of books - use the values().