我正在构建一个 ASP.NET C# 网站,并且我有一个下拉列表,我将其绑定到我创建的对象列表。绑定下拉列表的代码如下所示:
protected void PopulateDropdownWithObjects(DropDownList dropdownlist, List<myObject>() myObjects)
{
dropdownlist.DataValueField = "ID";
dropdownlist.DataTextField = "Name";
dropdownlist.DataSource = myObjects; // my code fails here
dropdownlist.DataBind();
}
但是,当它到达方法中的第 3 行时,会引发异常:
DataBinding: 'myObject' does not contain a property with the name 'ID'.
但是,我可以在调试时清楚地看到 myObject.ID 值:我可以在“立即”窗口中访问它,它是公共的,它不为空,并且我拼写正确且大小写正确:
public class myObject
{
public int ID; // see? "ID" is right here!
public string Name;
public myObject(
int id,
string name
)
{
this.ID = id;
this.Name = name;
}
}
还有其他可能导致此错误的原因吗?