查询viewmodel导致错误

时间:2014-11-30 17:27:37

标签: c# asp.net-mvc-4

查询我的viewmodel时出现此问题。我有一个索引页面,通过只显示标题,日期和作者来显示表格中的帖子。当点击标题时,它应该打开另一个页面,显示该帖子的主体内容。我是通过在索引页面@Html.ActionLink(item.PostTitle, "ReadTopic", new { PostID = @item.PostID })中指定以下内容来实现此目的的,并且这会给出网址http://localhost:50168/Posts/ReadTopic?PostID=1004。  在PostsController中,我设置了一个actionResult ReadTopic来查询名为ListPostVM的ViewModel

public class ListPostsVM
    {
        public int PostID { get; set; }

        public string PostTitle { get; set; }


        public string PostContent { get; set; }

        public DateTime PostDate { get; set; }

        public string Username { get; set; }
    }

并且在控制器中我设置了一个名为PostID的参数,以便根据url中的查询字符串查询模型,这是PostID所以我只能检索一个帖子,因为ID是唯一的,这意味着我只期待一个结果。

public ActionResult ReadTopic(int PostID)
{

    var query = from p in db.Post
                 join u in db.User
                 on p.UserID equals u.UserID
                 where p.PostID == PostID
                 select new ListPostsVM()
                 {
                     PostTitle = p.PostTitle,
                     PostContent = p.PostContent,
                     PostDate = p.PostDate,
                     Username = u.Username

                 };
    return View(query);

在我看来,我有以下

@model Bloggosphere.ViewModels.PostsComments



@{
    ViewBag.Title = "ReadTopic";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>ReadTopic</h2>

       <h1>@Model.PostTitle</h1>
        <p>@Model.PostContent</p>
        <h6>@Model.PostAuthor on @Model.PostDate</h6>

Wheneve我点击一个主题带我到ReadTopic视图,这样我就可以阅读我点击的标题正文我得到了这个错误

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[Bloggosphere.ViewModels.ListPostsVM]', but this dictionary requires a model item of type 'Bloggosphere.ViewModels.PostsComments'.

所以我尝试了下面的另一种方法

@model  IEnumerable<Bloggosphere.ViewModels.PostsComments>




@{
    ViewBag.Title = "ReadTopic";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>ReadTopic</h2>
    @foreach(var item in Model)
    {
        <h1>@item.PostTitle</h1>
        <p>@item.PostContent</p>
        <h6>@item.PostAuthor on @item.PostDate</h6>
    }

这也导致了

以下的错误
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[Bloggosphere.ViewModels.ListPostsVM]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Bloggosphere.ViewModels.PostsComments]'.

请问我做错了什么?我怎么能解决这个问题,我想要做的就是在网址中检索一个带有该ID的帖子?

1 个答案:

答案 0 :(得分:0)

我认为问题在于您的查询。如果您想获得一篇帖子,那么您应该在查询中执行FirstSingle

 var query = from p in db.Post
                 join u in db.User
                 on p.UserID equals u.UserID
                 where p.PostID == PostID
                 select new ListPostsVM()
                 {
                     PostTitle = p.PostTitle,
                     PostContent = p.PostContent,
                     PostDate = p.PostDate,
                     Username = u.Username

                 };
    return View(query.Single());

然后在您的视图中,您可以将model设置为ListPostsVM

@model Bloggosphere.ViewModels.ListPostsVM

如果要查询多个表,可以执行以下操作:

 var query = from p in db.Post.Where(p => p.PostID == PostID)
             from comments in db.Comments.Where(p => p.PostID == PostID)
                 join u in db.User
                 on p.UserID equals u.UserID
                 where p.PostID == PostID
                 select new ListPostsVM()
                 {
                     PostTitle = p.PostTitle,
                     PostContent = p.PostContent,
                     PostDate = p.PostDate,
                     Username = u.Username
                     Comments = comments
                 };
    return View(query.Single());