我有一个模板:
public class OwnedProvinces
{ public Guid ProvinceID;
public string ProvinceName;
}
我在这里用这个模板创建了一个列表:
List<OwnedProvinces> getinfo = (from upi in db.Utopia_Province_Data_Captured_Gens
where upi.Owner_User_ID == SQLStatementsCS.UserID()
where upi.Province_Name != string.Empty
select new OwnedProvinces { ProvinceName = upi.Province_Name, ProvinceID = upi.Province_ID}).ToList();
当我尝试将其绑定到Dropdown列表时出现此问题:
ddlSelectProvince.DataTextField = "ProvinceName";
ddlSelectProvince.DataValueField = "ProvinceID";
ddlSelectProvince.DataSource = getinfo;
ddlSelectProvince.DataBind();
它抛出错误:
DataBinding:'OwnedProvinces'不包含名为'ProvinceName'的属性。
基本上,它无法在List中找到属性ProvinceName,但对我没有意义。如果我进行匿名查询,它可以工作,但是当我将它分配给OwnedPRovinces类时,它会抛出此错误......
答案 0 :(得分:2)
尝试更改此类
public class OwnedProvinces
{
public Guid ProvinceID { get; set; }
public string ProvinceName { get; set; }
}
问题是省ID和省名是成员变量而不是属性。
答案 1 :(得分:1)
问题在于您的声明 -
public Guid ProvinceID;
public string ProvinceName;
是字段,但不是属性。您应该更改其定义,或者您应该尝试实现IDataBindable接口,如下所示: http://www.primaryobjects.com/CMS/Article95.aspx
#region IDataBindable Members
public List<NameValueType> ToList()
{
List<NameValueType> resultList = new List<NameValueType>();
using (DataContext context = new DataContext())
{
List<Monster> itemList = context.Monsters.ToList();
foreach (Monster item in itemList)
{
resultList.Add(new NameValueType(item.MonsterId.ToString(), item.Name));
}
}
return resultList;
}
#endregion