在 Asp.net mvc4 中获取单个帖子以及与该帖子相关的所有评论

本文关键字:评论 mvc4 net Asp 获取 单个帖 | 更新日期: 2023-09-27 18:33:36

大家好, 我目前面临一个查询问题,该查询使用查询字符串中的id参数从数据库中获取单个帖子并检索属于该帖子的所有评论。到目前为止,我知道我想要一个帖子,因此我的观点将不包含 IEnumerable 接口,因为在我的查询中我使用 .single((。但是,由于我正在从数据库中获取注释列表,而不仅仅是一条评论,我知道这将是 IEnumerable。我的问题是如何组合两个界面,以便我可以显示该单个帖子和属于它的所有评论。

 public class ReadPostsVM
{
    public string PostTitle { get; set; }
    public string PostContent { get; set; }
    public string PostAuthor { get; set; }
    public DateTime PostDate { get; set; }
    public string Comment { get; set; }
    public string CommentAuthor { get; set; }
    public DateTime CommentDate { get; set; }

}

这是我在控制器中尝试的

public ActionResult ReadPost(int id)
{
    var query = from p in db.Posts.Where(p => p.PostId == id)
                from c in db.Replies.Where(c => c.PostId == id)
                join u in db.UserProfiles
                on p.AuthorId equals u.UserId
                where p.PostId == id
                select new ReadPostsVM()
                {
                    PostTitle = p.PostTitle,
                    PostContent = p.PostContent,
                    PostDate = p.DateCreated,
                    PostAuthor = u.UserName,
                    Comment = c.Reply1,
                    CommentAuthor = u.UserName,
                    CommentDate = c.ReplyDate
                };
    var query = from p in db.Posts
                join u in db.UserProfiles
                on p.AuthorId equals u.UserId
                where p.PostId == id
                select new ReadPostsVM()
                {
                    PostTitle = p.PostTitle,
                    PostContent = p.PostContent,
                    PostDate = p.DateCreated,
                    PostAuthor = u.UserName,
                };
    return View(query.Single());

}

然而,在我的观点中

@model IEnumerable<Blogger.ViewModels.ReadPostsVM>

还有一个foreach循环,给了我一个错误说

Sequence does not contain any element

我认为我有的另一个选择是在下面获取这个

public ActionResult ReadPost(int id)
    {
        var query = from p in db.Posts
                    join u in db.UserProfiles
                    on p.AuthorId equals u.UserId
                    where p.PostId == id
                    select new ReadPostsVM()
                    {
                        PostTitle = p.PostTitle,
                        PostContent = p.PostContent,
                        PostDate = p.DateCreated,
                        PostAuthor = u.UserName,
                    };
        return View(query.Single());

    }

获取该单个帖子,以仅返回单个帖子,也许我可以使用"部分"视图来显示评论。

@model Blogger.ViewModels.ReadPostsVM

请帮助我如何实现这一目标。如果我要使用部分视图,我该如何使该部分视图根据 url 中的参数获取评论,因为我将不得不使用 id 参数调用操作,但我没有任何指向该页面的链接,而且我认为这样做没有意义。我希望我的问题有意义?请帮忙!

为了显示帖子实体和评论实体之间的关系,我在下面添加了帖子.cs类和回复.cs类

public partial class Post
{
    public Post()
    {
        this.Replies = new HashSet<Reply>();
    }
    public int PostId { get; set; }
    public string PostTitle { get; set; }
    public string PostContent { get; set; }
    public int AuthorId { get; set; }
    public int CategoryId { get; set; }
    public System.DateTime DateCreated { get; set; }
    public virtual Category Category { get; set; }
    public virtual UserProfile UserProfile { get; set; }
    public virtual ICollection<Reply> Replies { get; set; }
}

 public partial class Reply
    {
        public int ReplyId { get; set; }
        public string Reply1 { get; set; }
        public System.DateTime ReplyDate { get; set; }
        public int AuthorId { get; set; }
        public int PostId { get; set; }
        public int CategoryId { get; set; }
        public virtual Category Category { get; set; }
        public virtual Post Post { get; set; }
        public virtual UserProfile UserProfile { get; set; }
    }

在 Asp.net mvc4 中获取单个帖子以及与该帖子相关的所有评论

您需要更改视图模型,以便发布视图模型包含一组评论

查看模型

public class CommentVM
{
  public string Comment { get; set; }
  public string CommentAuthor { get; set; }
  public DateTime CommentDate { get; set; }
}
public class PostVM
{
  public string PostTitle { get; set; }
  public string PostContent { get; set; }
  public string PostAuthor { get; set; }
  public DateTime PostDate { get; set; }
  List<CommentVM> Comments { get; set; }
}

控制器(我会把它留给你隐蔽的LINQ To SQL(

public ActionResult ReadPost(int id)
{
  Post post = db.Posts.Find(id);
  PostVM model = new PostVM()
  {
    PostTitle = post.PostTitle,
    PostContent = post.PostContent,
    PostAuthor = post.UserProfile.UserName,
    PostDate = post.DateCreated,
    Comments = post.Replies.Select(r => new CommentVM()
    {
      Comment = r.Reply1,
      CommentAuthor = r.UserProfile.UserName,
      CommentDate = r.ReplyDate
    })
  };
  return View(model);
}

视图

@model PostVM
...
@DisplayFor(m => m.PostTitle)
...
@DisplayFor(m => m.PostDate )
@foreach(var comment in Model.Comments)
{
  @DisplayFor(comment.Comment)
  ...
  @DisplayFor(comment.CommentDate)
}

你可以看看像自动映射器这样的东西来简化你的映射。否则,为了简单起见,在您的视图中模型具有类似以下内容:

public IEnumerable<comment> comments { get; set; }

然后有一个正确填充它的方法:

 select new ReadPostsVM()
                    {
                        PostTitle = p.PostTitle,
                        PostContent = p.PostContent,
                        PostDate = p.DateCreated,
                        PostAuthor = u.UserName,
                        PostReplies = p.Replies
                    };

然后,您将能够通过循环浏览 PostReply 来访问视图中的回复信息:

@foreach (var reply in Model.PostReplies)
 {
<div>reply.Reply1</div>
<div>reply.UserProfile.name</div>
}