public class BlogPost { public int BolgID { get; set; } public int ID { get; set; } public string Title { get; set; } public virtual ICollection<Category> Category { get; set; } public virtual BlogSite BlogSite { get; set; } }
public class Category { public int ID { get; set; } public string Title { get; set; } public virtual ICollection<BlogPost> BlogPost { get; set; }
using(MyDemoContext context = new MyDemoContext()) { DbSet<BlogPost> post = context.Set<BlogPost>(); //查出某篇博客的多种分类 var v = post.Include(p=>p.Category).Where(p=>p.ID==5).ToList(); Repeater1.DataSource = v; Repeater1.DataBind(); }
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table> <thead> <td>BlogID</td> <td>BlogTitle</td> <td>CategoryTitle</td>//Category的标题 </thead> </HeaderTemplate> <ItemTemplate> <tr> <td><%# Eval("ID")%></td> <td><%# Eval("Title")%></td> <td><%# Eval("")%></td>//这里怎么绑定 </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater>
最终显示效果就是列出这篇博客对应的多种分类:
BlogID BlogTitle CategoryTitle 5 test C# 5 test asp.net 5 test 技术
7 回答
繁星点点滴滴
TA贡献1803条经验 获得超3个赞
1.可以不用repeater啊。直接两个循环嵌套。
DbSet<BlogPost> post = context.Set<BlogPost>();
foreach(var item in post)
{
foreach(var category in item.Category.CreateSourceQuery())
{
%>
<tr>
<td><%=item.Title%></td>
<td><%=category.Title%></td>
</tr>
<%
}
)
}
2或者后台两张表join查询,得到一个list,然后repeater绑定这个list
- 7 回答
- 0 关注
- 594 浏览
添加回答
举报
0/150
提交
取消