0

我正在创建一个页面,该页面将显示过去 7 天内创建的 6 个博客。该博客包含一张图片和多条评论

这是博客模型

public class Blog
{
    public int BlogID { get; set; }
    public int likes { get; set; }
    public int ImageID { get; set; }
    public Boolean removed { get; set; }
    public DateTime dte_created { get; set; }
    public DateTime? dte_modified { get; set; }
    public virtual Image Image { get; set; }
    public virtual ICollection<Comment> Comment { get; set; }
}

这是博客内容。

public class Image
{
    public int ImageID { get; set; }
    public string img_path { get; set; }
    public string Description { get; set; }       
    public DateTime dte_created { get; set; }
}

public class Comment
{
    public int CommentID { get; set; }
    public string Commentation { get; set; }
    public int likes { get; set; }
    public int BlogID { get; set; }
    public DateTime dte_created { get; set; }
    public DateTime? dte_modified { get; set; }
    public Boolean removed { get; set; }
    //public virtual int Account_ID { get; set; }
    public virtual Blog Blog { get; set; }
}

这是控制器

private ACaptureDB db = new ACaptureDB();        

public ViewResult Index()
{
    ViewBag.Message = "ArchiCapture";
    var dateCheck = DateTime.Now.AddDays(-7);

    var results = from r in db.Blog
                  where r.dte_created >= dateCheck
                  select r;

    return View(results);
}

和我的观点。

@model IEnumerable<ACapture.Models.Blog>

@{
    ViewBag.Title = "Home Page";
}


<div class="Forum">
    <p>The Forum</p>

    <form class="Forum" runat="server">
            @foreach (var item in Model)
            {

                <div class="ForumChild"><img src="@item.Image.img_path" alt="Not Found" />
                <br />
                    <table>
                        ?????????
                    </table>
                </div>                

            }
    </form>
</div>

我将如何检索链接到博客的所有评论?

4

1 回答 1

2

您应该能够遍历模型博客对象中的 Comments 集合:

<table>
    @foreach(var comment in Model.Comments) {
         <tr>
            <td>@comment.Commentation</td>
            ....
         </tr>
    }
</table>
于 2012-07-31T18:02:13.473 回答