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 技術(shù)
7 回答

繁星點點滴滴
TA貢獻1803條經(jīng)驗 獲得超3個贊
1.可以不用repeater啊。直接兩個循環(huán)嵌套。
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 關(guān)注
- 639 瀏覽
添加回答
舉報
0/150
提交
取消