取一个与后所有评论有关该职位在Asp.net mvc4沿 [英] Fetching a single post along with all comments relating to that post in Asp.net mvc4

查看:92
本文介绍了取一个与后所有评论有关该职位在Asp.net mvc4沿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
             我psently面临的一个问题$ P $与使用id参数的查询字符串从数据库中获取一个职位,也找回属于该职位所有评论的查询。到目前为止,我知道,我希望有一个单一的职位,因此我认为不会包含IEnumerable接口,因为我的查询我使用。单()。但是,由于我是从数据库中获取的评论列表,不只是一个评论我知道这将是IEnumerable的。我的问题是如何将两者结合起来的接口,这样我可以显示单一的职位和属于它的所有意见。

 公共类ReadPostsVM
{
    公共字符串PostTitle {搞定;组; }    公共字符串PostContent {搞定;组; }    公共字符串PostAuthor {搞定;组; }    公众的DateTime踵{搞定;组; }    公共字符串评论{搞定;组; }    公共字符串CommentAuthor {搞定;组; }    公众的DateTime CommentDate {搞定;组; }
}

这是我试图在控制器

 公众的ActionResult ReadPost(INT ID)
{
    VAR的查询=从db.Posts.Where P(P => p.PostId == ID)
                从C在db.Replies.Where(C => c.PostId == ID)
                走进你的db.UserProfiles
                在p.AuthorId等于u.UserId
                其中,p.PostId == ID
                选择新ReadPostsVM()
                {
                    PostTitle = p.PostTitle,
                    PostContent = p.PostContent,
                    踵= p.DateCreated,
                    PostAuthor = u.UserName,
                    注释= c.Reply1,
                    CommentAuthor = u.UserName,
                    CommentDate = c.ReplyDate
                };    VAR查询从db.Posts P =
                走进你的db.UserProfiles
                在p.AuthorId等于u.UserId
                其中,p.PostId == ID
                选择新ReadPostsVM()
                {
                    PostTitle = p.PostTitle,
                    PostContent = p.PostContent,
                    踵= p.DateCreated,
                    PostAuthor = u.UserName,                };    返回查看(query.Single());
}

然而在视图中我有

  @model IEnumerable的< Blogger.ViewModels.ReadPostsVM>

和这给了我一个错误foreach循环话说

 序列中不包含任何元素

我觉得我有另一种选择是只此获取以下

 公众的ActionResult ReadPost(INT ID)
    {        VAR查询从db.Posts P =
                    走进你的db.UserProfiles
                    在p.AuthorId等于u.UserId
                    其中,p.PostId == ID
                    选择新ReadPostsVM()
                    {
                        PostTitle = p.PostTitle,
                        PostContent = p.PostContent,
                        踵= p.DateCreated,
                        PostAuthor = u.UserName,                    };        返回查看(query.Single());
    }

要获得使用只返回一个职位,一个职位,也许我就可以使用一个局部视图来显示评论。

  @model Blogger.ViewModels.ReadPostsVM

请帮我我怎么能达到这个目的。如果我使用的局部视图我怎么能作出这样的局部视图,以基于URL的参数提取评论,因为我将不得不调用与id参数的行动,但我没有任何链接指向该网页,还我不认为它将使感这样。我希望我的问题是有道理的?请帮助!

要显示的帖子实体,我添加了Posts.cs类和Replies.cs类以下

的意见实体之间的关系

 公共部分类帖子
{
    公共邮政()
    {
        this.Replies =新的HashSet<回覆与GT;();
    }    公众诠释{帖子ID获取;组; }
    公共字符串PostTitle {搞定;组; }
    公共字符串PostContent {搞定;组; }
    公众诠释AUTHORID {搞定;组; }
    公众诠释的CategoryId {搞定;组; }
    公众的System.DateTime dateCreated会获得{;组; }    公共虚拟分类分类{搞定;组; }
    公共虚拟用户配置用户配置{搞定;组; }
    公共虚拟的ICollection<回覆与GT;回复{搞定;组; }
} 公共部分类回复
    {
        公众诠释ReplyId {搞定;组; }
        公共字符串REPLY1 {搞定;组; }
        公众的System.DateTime ReplyDate {搞定;组; }
        公众诠释AUTHORID {搞定;组; }
        公众诠释{帖子ID获取;组; }
        公众诠释的CategoryId {搞定;组; }        公共虚拟分类分类{搞定;组; }
        公共虚拟文章张贴{搞定;组; }
        公共虚拟用户配置用户配置{搞定;组; }
    }


解决方案

您需要更改您的视图模型,使后视图模型包含注释集合

查看模型

 公共类CommentVM
{
  公共字符串评论{搞定;组; }
  公共字符串CommentAuthor {搞定;组; }
  公众的DateTime CommentDate {搞定;组; }
}公共类PostVM
{
  公共字符串PostTitle {搞定;组; }
  公共字符串PostContent {搞定;组; }
  公共字符串PostAuthor {搞定;组; }
  公众的DateTime踵{搞定;组; }
  清单< CommentVM>评论{搞定;组; }
}

控制器(我会向你要隐蔽LINQ to SQL的离开吧)

 公众的ActionResult ReadPost(INT ID)
{
  后后= db.Posts.Find(ID);
  PostVM模式=新PostVM()
  {
    PostTitle = post.PostTitle,
    PostContent = post.PostContent,
    PostAuthor = post.UserProfile.UserName,
    踵= post.DateCreated,
    评论= post.Replies.Select(R = GT;新CommentVM()
    {
      注释= r.Reply1,
      CommentAuthor = r.UserProfile.UserName,
      CommentDate = r.ReplyDate
    })
  };
  返回查看(模型);
}

查看

  @model PostVM
...
@DisplayFor(M => m.PostTitle)
...
@DisplayFor(M => m.PostDate)@foreach(在Model.Comments VAR评论)
{
  @DisplayFor(comment.Comment)
  ...
  @DisplayFor(comment.CommentDate)
}

Hello Everyone, I am facing a problem presently with a query that uses the id parameter in the query string to fetch a single post from the database and also retrieve all comments that belong to the posts. So far i know that i want a single post hence my view will not contain the IEnumerable interface because in my query i use .single(). However since i'm fetching a list of comments from the database and not just one comment i know that will be IEnumerable. My problem is how to combine both interfaces so that i can display that single post and all comments that belong to it .

 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; }


}

This is what i tried in the controller

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());


}

However in the view i have

@model IEnumerable<Blogger.ViewModels.ReadPostsVM>

and a foreach loop which gave me an error saying

Sequence does not contain any element

Another option i think i have is to fetch just this below

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());


    }

to get that single post using to return just a single post and maybe i can then use a Partial view to display the comments.

@model Blogger.ViewModels.ReadPostsVM

Please help me out how can I achieve this aim. If i am to use a partial view how can i make that partial view to fetch comments based on the parameter in the url since i will have to invoke an action with an id parameter but i dont have any link pointing to that page and also i dont think it would make sense like that. I hope my question makes sense? Please help!

To show the relationship between the Posts entity and the comments entity i have added the Posts.cs class and Replies.cs class below

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; }
    }

解决方案

You need to change your view models so that the Post view model contains a collection of Comments

View models

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; }
}

Controller (I'll leave it to you covert to 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);
}

View

@model PostVM
...
@DisplayFor(m => m.PostTitle)
...
@DisplayFor(m => m.PostDate )

@foreach(var comment in Model.Comments)
{
  @DisplayFor(comment.Comment)
  ...
  @DisplayFor(comment.CommentDate)
}

这篇关于取一个与后所有评论有关该职位在Asp.net mvc4沿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆