我正在将一对列表数据绑定(bind)到下拉列表,由于某种原因它不起作用,我很感兴趣。
我正在使用的代码是:
public void BindDropDown(List<Pair> dataList)
{
ddlGraphType.DataTextField = "First";
ddlGraphType.DataValueField = "Second";
ddlGraphType.DataSource = dataList;
ddlGraphType.DataBind();
}
我得到了这个异常(exception),这是一个谎言!
DataBinding: 'System.Web.UI.Pair' does not contain a property with the name 'First'.
提前致谢。
已添加
我知道异常的含义,但是 pair 对象确实包含 First 和 Second 属性,这就是问题所在。
最佳答案
First
和 Second
是字段而不是 Pair
的属性类型。您需要创建一个具有两个属性的类:
public class NewPair
{
public string First { get; set; }
public string Second { get; set; }
}
编辑:使用 Tuple :@Damien_The_Unbeliever 和 @Chris Chilvers 建议
List<Tuple<string, string>> list = new List<Tuple<string, string>>()
{
new Tuple<string,string>("One","1"),
new Tuple<string,string>("Two","2"),
};
ddlGraphType.DataTextField = "Item1";
ddlGraphType.DataValueField = "Item2";
ddlGraphType.DataSource = list;
ddlGraphType.DataBind();
关于c# - 数据绑定(bind) : 'System.Web.UI.Pair' does not contain a property with the name 'First' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8257697/