[英]include 3rd level related collection and display in gridview with code-first entity framework
I have the following template fields in my gridview (ItemType attribute is set properly: ItemType="gEchoLu.Model.DiscussionTimeframe"
): 我的gridview中有以下模板字段(ItemType属性设置正确:
ItemType="gEchoLu.Model.DiscussionTimeframe"
):
<asp:TemplateField HeaderText="Discussion Wall">
<ItemTemplate>
<asp:Label runat="server" ID="lbl_Discussion" Text="<%#Item.DiscussionWall.WallTitle %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course">
<ItemTemplate>
<asp:Label runat="server" ID="lbl_Discussion" Text="<%#Item.DiscussionWall.Course.CourseTitle %>"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The first field is displayed correctly. 第一个字段显示正确。 However, the second template field is always null.
但是,第二个模板字段始终为空。 Here is the code by which I bind data to the gridview:
这是将数据绑定到gridview的代码:
gEchoLuDBContext db = new gEchoLuDBContext();
var timeframes = db.Timeframes.Include(c => c.DiscussionWall.Course).Where(tf => tf.CreatedBy == userid).ToList();
grd_Timeframes.DataSource = timeframes;
grd_Timeframes.DataBind();
When I debug it, the properties of Course is null actually. 当我调试它时,Course的属性实际上为空。 In my model, each
TimeFrame
belongs to a DiscussionWall
, and each DiscussionWall
belongs to a Course
. 在我的模型中,每个
TimeFrame
属于一个DiscussionWall
,而每个DiscussionWall
属于Course
。 However, there is no direct relationship between a TimeFrame
and a Course
. 但是,
TimeFrame
和Course
之间没有直接关系。 I am not sure if what I want is doable in this situation. 我不确定在这种情况下我想要的是否可行。 Do I have to define a (1-to-many) relationship between
TimeFrame
an DiscussionWall
entities to achieve what I want? 我是否必须在
TimeFrame
和DiscussionWall
实体之间定义(一对多)关系才能实现我想要的? (I know this can be done by using OnDataBound
, but I wonder if ItemType
can be used to handle this quickly). (我知道可以使用
OnDataBound
完成此操作,但我想知道ItemType
可以用于快速处理此问题)。
Here are the related parts of my model: 这是我模型的相关部分:
public class DiscussionWall
{
[Key]
public int WallId { get; set; }
[Required]
[StringLength(200)]
public string WallTitle { get; set; }
[Required]
[ForeignKey("Course")]
public int CourseId { get; set; }
private Course _course;
public Course Course
{
get { return _course?? (_course = new Course());}
set { _course = value; }
}
public List<DiscussionTimeframe> Timeframes { get; set; }
}
public class DiscussionTimeframe
{
public int DiscussionTimeframeId { get; set; }
[Required]
public string TimeFrameName { get; set; }
public int DiscussionWallId { get; set; }
[ForeignKey("DiscussionWallId")]
public DiscussionWall DiscussionWall { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string CourseTitle { get; set; }
public List<DiscussionWall> DiscussionWalls { get; set; }
}
It worked when I change this: 当我更改此设置时,它起作用了:
private Course _course;
public Course Course
{
get { return _course?? (_course = new Course());}
set { _course = value; }
}
into this: 到这个:
public Course Course
{
get;
set;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.