C# 在ASP.NET MVC中显示LINQ查询的结果

C# 在ASP.NET MVC中显示LINQ查询的结果,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我有一个包含LINQ查询的控制器。我的查询如下所示: var posts = GetBlogPosts(); var results = from post in posts select new { Title = "This is the blog title", Url = "www.google.com", Author = "Bill

我有一个包含LINQ查询的控制器。我的查询如下所示:

var posts = GetBlogPosts();
var results = from post in posts
              select new
              {
                Title = "This is the blog title",
                Url = "www.google.com",
                Author = "Bill Jones",
                AuthorUrl = "www.billjones.com",
                Description = "This is the description of the post"
              };
ViewBag.Posts = results;
@foreach (var post in ViewBag.Posts)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}
我试图在我的视图中显示帖子。目前,我有以下几点:

var posts = GetBlogPosts();
var results = from post in posts
              select new
              {
                Title = "This is the blog title",
                Url = "www.google.com",
                Author = "Bill Jones",
                AuthorUrl = "www.billjones.com",
                Description = "This is the description of the post"
              };
ViewBag.Posts = results;
@foreach (var post in ViewBag.Posts)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}

我做错了什么?

这是因为这部分代码选择了匿名对象。视图不知道这是什么类型的对象。您的Viewbag也是一个动态对象,它不会告诉视图它所处的对象的特定类型

          select new
          {
            Title = "This is the blog title",
            Url = "www.google.com",
            Author = "Bill Jones",
            AuthorUrl = "www.billjones.com",
            Description = "This is the description of the post"
          };
很可能您可以执行以下操作

 ViewBag.Posts = GetBlogPosts();
在您看来,您可以这样做:

@foreach (var post in (IEnumerable<Post>)ViewBag.Posts)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}
@foreach(在(IEnumerable)ViewBag.Posts中的var post)
{
@职务

@post.Description

}
或者最好在页面顶部放置以下内容,创建强类型视图

 @model IEnumerable<Post>
@model IEnumerable
在控制器中返回 查看(GetBlogPosts())

在你看来,你是这样做的

@foreach (var post in Model)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}
@foreach(模型中的var post)
{
@职务

@post.Description

}
您可以使用局部视图,而不是使用ViewBag,因为您可以使用ViewBag。您需要在控制器内创建新操作,然后使用HTML.RenderAction从父视图调用该操作,如下所示 1-创建新操作以包含Linq代码

[ChildActionOnly]
    public ViewResult GetPosts()
    {
    var posts = GetBlogPosts();
    var results = from post in posts
                  select new
                  {
                    Title = "This is the blog title",
                    Url = "www.google.com",
                    Author = "Bill Jones",
                    AuthorUrl = "www.billjones.com",
                    Description = "This is the description of the post"
                  };
    return PartialView("ViewName,posts); // the partial view name ,with posts object
}
2-现在用(Posts)类型的强类型视图创建一个局部视图,并给它取任何名称,然后像编写代码一样编写代码,只需稍作修改

@model IEnumerable<Post>
@foreach (var post in Model)
{
  <div class="postBody">
    <h1 class="blogTitle">@post.Title</h1>
    <p class="blogInfo">@post.Description</p>
  </div>
}
@model IEnumerable
@foreach(模型中的var post)
{
@职务

@post.Description

}
3-现在在父视图中添加HTML.RenderAction

    <html>
<head>/<head>
    <body>
    ...
    <div>
    @ { Html.RenderAction("GetPosts");  }  <!--ActionName -->
    </div>
    ...
    </body>
</html>

/
...
@{Html.RenderAction(“GetPosts”);}
...
有关更多信息,请查看以下链接


我希望帮助

Post定义应该放在哪里?我在Models文件夹中创建了一个类。但是,我的.cshtml文件仍然找不到它。我收到一个错误,上面写着:“CS0246:找不到类型或命名空间名称'Post'。”您可以使用从GetBlogPosts()返回的完整命名空间和类型名称例如,如果您的GetBlogPosts()返回一个列表,那么将其放入@Model List