2014-03-24 53 views
0

我有一个radcombobox,我试图将选定的值绑定到我从数据库中的表中拉回来的数据。RadComboBox绑定选定的值

<telerik:RadComboBox ID="cboRole" runat="server" DataValueField="UserType.ID" DataTextField="UserType.Value" Text='<%# Bind("UserType") %>' DataSourceID="odsCertifications" > 

            </telerik:RadComboBox> 

这里是我的数据

public partial class Certification 
{ 
    public Certification() 
    { 
     this.Courses = new HashSet<CertificationCourse>(); 
    } 

    public int ID { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    public int Duration { get; set; } 
    public Nullable<System.DateTime> Expiration { get; set; } 
    public bool IsActive { get; set; } 
    public string CreatedBy { get; set; } 
    public System.DateTime CreatedOn { get; set; } 
    public string UpdatedBy { get; set; } 
    public Nullable<System.DateTime> UpdatedOn { get; set; } 
    public Nullable<int> UserTypeID { get; set; } 

    public virtual ICollection<CertificationCourse> Courses { get; set; } 
    public virtual SystemCode UserType { get; set; } 
} 

UserType具有的属性值和ID赖以我想绑定的DataTextFieldsDataValueFields。但是,当我尝试以这种方式绑定时,我收到了JavaScript异常。 javascript消息如果省略,所以我无法完全看到确切的错误消息。

这里是铬JS调试器告诉我发生的事情:

Uncaught Sys.WebForms.PageRequestManagerServerErrorException: 
Sys.WebForms.PageRequestManagerServerErrorException: Object of type 
System.Data.Entity.DynamicProxies.Certification_D985D293565CB0BFD87FA8F8F44D70ACF4130D6E138A82D6E486544C53D7FE10 does ...<omitted>...y. 

<asp:ObjectDataSource runat="server" ID="odsCertifications" SelectMethod="GetActiveCertifications" TypeName="SitefinityWebApp.Controllers.CertificationController"></asp:ObjectDataSource> 


    public List<Certification> GetActiveCertifications() 
    { 
     using (var db = new Entities()) 
     { 
      try 
      { 

       return db.Certifications.Include("UserType").Where(x => x.IsActive == true).OrderBy(x => x.Title).ToList(); 

      } 

      catch (Exception ex) 
      { 
       ExceptionManager.LogException(this, ex); 
       return null; 
      } 
     } 
    } 

回答

1

我没有在Telerik的的RAD版本的任何知识,但我认为这个问题来自于事实,你是试图将一个SystemCode对象绑定到ComboBox,并且它只能处理像int/string这样的简单类型。
我想你应该与用户类型ID相反,但我不知道它会工作,因为它是可空的。

0

你想要做什么?我想“odsCertifications”不是一个列表。所以如果你想只显示认证对象中的绑定用户类型值。您不需要将任何来源绑定到组合框。只需在cs中将列表项添加到组合框。侧面,如:

cbcRole.Items.Add(new RadComboboxItem(){Value=odsCertifications.UserType.ID, Text=odsCertifications.UserType.Value}) //RadComboboxItem and properties can be different I wrote only as example. 

如果你想和所有的用户类型和显示绑定用户类型值,它是在认证对象选定项目来填充组合框,你必须要用户类型列表对象绑定到你的组合框,如:

<telerik:RadComboBox ID="cboRole" runat="server" DataValueField="ID" DataTextField="Value" DataSourceID="userTypes" ></telerik:RadComboBox> 

的DataSourceID = “userTypes” 在这里,userTypes是必须像

List<UserType> userTypes; 

比你可以在CS组选定的项目列表对象。方如:

cboRole.SelectedValue=odsCertifications.UserType.ID; 
0

要获得实际的服务器异常,请删除AJAX。你得到的是MS AJAX在部分回发期间捕获的服务器异常,并将其作为JavaScript错误发送(截断)给客户端。

我也认为你可能需要暴露你需要的字符串属性作为直接字段,而不是你传递给组合属性的字段的子字段