代码优先开发使用EF多对多如何绑定数据到repeater?
BlogPost和Category多关系,如果数据集直接绑定repeater,现在如何使用先开发的代码来绑定repeater?
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 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();
}
博客ID 博客标题 CategoryTitle//分类标题
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ID")%></td>
<td><%# Eval("Title")%></td>
<td><%# Eval("")%></td>//here how to bind Category.Ttitle?
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
如何得到这个结果:
BlogID BlogTitle CategoryTitle
5 test C#
5 test asp.net
5 test VB
BlogPost and Category-many relationship, if the dataset directly bind repeater, now how to use code first developed to bind repeater?
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 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();
}
BlogID
BlogTitle
CategoryTitle//Category's title
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("ID")%></td>
<td><%# Eval("Title")%></td>
<td><%# Eval("")%></td>//here how to bind Category.Ttitle?
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
How to get this result:
BlogID BlogTitle CategoryTitle
5 test C#
5 test asp.net
5 test VB
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类别和BlogPost具有多对多的关系,因此博客文章上会有多个类别。我不确定您想在此处显示哪个类别标题 - 通常,我可能会显示逗号分隔的列表(或者可能是项目符号列表),这可以通过嵌套转发器来实现。例如,
声明:未经测试的代码
上面的模板将生成逗号分隔的列表,但您可以根据需要调整布局。
Catgory and BlogPost has many to many relation, so on blog post will have multiple categories. I am not sure which category title you want to show here - typically, I would probably show a comma separated list (or perhaps a bulleted list), this can be achieve by nesting a repeater. For example,
Diclaimer: untested code
Noe above template will generate comma seperated list but you can adjust layout as per your need.