我有一个IEnumerable<School>
的集合正在传递给扩展 方法,该方法将填充DropDownList
。我还想通过 DataValueField
和DataTextField
作为参数,但我希望它们是 强类型。将强类型属性名称作为参数传递
基本上,我不想为DataValueField
和DataTextField
参数传递string
,这很容易出错。
public static void populateDropDownList<T>(this DropDownList source,
IEnumerable<T> dataSource,
Func<T, string> dataValueField,
Func<T, string> dataTextField) {
source.DataValueField = dataValueField; //<-- this is wrong
source.DataTextField = dataTextField; //<-- this is wrong
source.DataSource = dataSource;
source.DataBind();
}
调用是这样的...
myDropDownList.populateDropDownList(states,
school => school.stateCode,
school => school.stateName);
我的问题是,我怎么能传递DataValueField
和DataTextField
强类型作为参数传递给populateDropDownList?
我不明白这些字段类型的字符串。 – jfin3204