代码优先开发使用EF多对多如何绑定数据到repeater?

发布于 2024-12-11 09:14:41 字数 1999 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2024-12-18 09:14:41

类别和BlogPost具有多对多的关系,因此博客文章上会有多个类别。我不确定您想在此处显示哪个类别标题 - 通常,我可能会显示逗号分隔的列表(或者可能是项目符号列表),这可以通过嵌套转发器来实现。例如,

<ItemTemplate>
   <tr>
     <td><%# Eval("ID")%></td>
     <td><%# Eval("Title")%></td>
     <td>
        <asp:Repeater runat="server" ID="C" DataSource='<%# Eval("Category") %>'>
           <ItemTemplate><%# Eval("Title") #></ItemTemplate>
           <SeparatorTemplate>, </SeparatorTemplate>
        </asp:Repeater>
     </td>
   </tr>
</ItemTemplate>

声明:未经测试的代码

上面的模板将生成逗号分隔的列表,但您可以根据需要调整布局。

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,

<ItemTemplate>
   <tr>
     <td><%# Eval("ID")%></td>
     <td><%# Eval("Title")%></td>
     <td>
        <asp:Repeater runat="server" ID="C" DataSource='<%# Eval("Category") %>'>
           <ItemTemplate><%# Eval("Title") #></ItemTemplate>
           <SeparatorTemplate>, </SeparatorTemplate>
        </asp:Repeater>
     </td>
   </tr>
</ItemTemplate>

Diclaimer: untested code

Noe above template will generate comma seperated list but you can adjust layout as per your need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文