我有这样的查询
var businessList = (from u in _DB.Users
join cat in _DB.BusinessCategories on u.UserId equals cat.UserId
select new BusinessDetails(u.Businesses.FirstOrDefault(), u)).Skip(0).Take(10);
dlBusiness.DataSource = businessList.ToList();
dlBusiness.DataBind();
我将Businessname作为Business类的成员。我有一个DataList,如下所示
<asp:DataList ID="dlBusiness" DataKeyField="UserId" runat="server" >
<ItemTemplate>
<%#Eval("Business.BusinessName")%>
</ItemTemplate>
</asp:DataList>
这是课程定义
public class BusinessDetails
{
public BusinessDetails(Business business,User user){
this._Business = business;
this._UserDetails = user;
}
private string _distance;
public string Distance
{
get { return _distance; }
set { _distance = value; }
}
private Business _Business;
public Business BusinessData
{
get { return _Business; }
set { _Business = value; }
}
private User _UserDetails;
public User UserDetails
{
get { return _UserDetails; }
set { _UserDetails = value; }
}
}
但这显示空白?如何将其绑定到DataList?
答案 0 :(得分:1)
我会把它改成
DataBinder.Eval(Container.DataItem, "BusinessData.BusinessName")
假设商务舱有BusinessName
属性?
答案 1 :(得分:1)
我想你想要
<%#Eval("BusinessData.BusinessName")%>
而不是
<%#Eval("Business.BusinessName")%>
ItemTemplate
标记中的。
我认为BusinessDetails
课上没有UserId字段。
您应该用唯一的键列名替换它或删除它。我看到你可以访问BussinessName
所以访问说明符是公共的。
绑定是正确的。如果dataList控件dlBusiness
找不到该属性,则抛出异常。
也许问题的原因来自businessList
返回空BussinessName的事实。