简体   繁体   English

C#MVC-访问模型列表中的项目

[英]C# MVC - Access Items in a List that is in a Model

I have a Blog that is populating from a BlogResponseModel, and the related comments for each Blog entry are held in a List. 我有一个从BlogResponseModel填充的Blog,每个Blog条目的相关注释都保存在List中。 It looks like this: 看起来像这样:

public class BlogResponseModel
{
    public int Id { get; set; }
    public DateTime SubmissionDate { get; set; }
    public string Title { get; set; }
    public string ImageID { get; set; }
    public string ImageAttribution { get; set; }
    public string Body { get; set; }
    public List<BlogCommentModel> CommentList { get; set; }
}

The comments are collected and each comment is related to the Id element above. 收集评论,每个评论与上面的Id元素相关。 The BlogCommentModel looks like this: BlogCommentModel看起来像这样:

public class BlogCommentModel
{
    public int CommentId { get; set; }
    public int CommentCount { get; set; }
    public DateTime CommentDate { get; set; }
    public string Commenter { get; set; }
    public string CommenterEmail { get; set; }
    public string Comment { get; set; }
    public int BlogParentId { get; set; }
}

In my view, I am using a foreach to format and display each Blog entry, and this is working very well. 在我看来,我正在使用一个foreach来格式化和显示每个Blog条目,并且它工作得很好。 Where I have hit the wall is figuring out how to embed the related comments at the foot of each Blog post. 我碰到的地方正在弄清楚如何在每个Blog帖子的底部嵌入相关评论。

Here is the current attempt in the View that has me stumped. 这是视图中目前困扰我的尝试。 I do a foreach to render the Blog post... 我做了一个foreach来呈现Blog帖子...

   @foreach (var response in Model)
   {
        // enter all the Blog specifics here...

At this point, the Blog entry is rendered, and we are ready to add the Comments: 此时,将呈现Blog条目,并且我们准备添加注释:

<div class="message row">
    <div>
        <H5>Comments</H5>
    </div>
    @foreach (var comment in ?? WHAT ?? (how do I access BlogResponseModel)
    {
        <div class="replies span12">
            <div class="reply">
                <div class="created pull-right">
                    @comment.CommentDate
                </div>
                <div class="created">
                    @comment.Commenter
                </div>
                <div>
                    @comment.Comment
                </div>
            </div>
        </div>
    }
</div>

How do I populate the Comments from List CommentList that is in the BlogResponseModel? 如何从BlogResponseModel中的列表CommentList中填充评论?

Thanks in advance for your advice. 预先感谢您的建议。 I'm new to MVC and still wrapping my mind around the architecture. 我是MVC的新手,但仍然对体系结构充满信心。

You can add 你可以加

public virtual ICollection<BlogCommentModel> CommentList { get; set; }

instead of 代替

public List<BlogCommentModel> CommentList { get; set; }

Then in the View 然后在视图中

@foreach (var item in Model)
{
    @Html.DisplayFor(modelItem => item.Title)

    @foreach (var myBlogCommentModel in @item.BlogCommentModel)
    {
        @myBlogCommentModel.Commenter
        @myBlogCommentModel.CommenterEmail
    }
}

I hope this will help. 我希望这将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM