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

时间:2014-02-02 10:46:50

标签: 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 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>
}

执行此操作时,我在运行时收到错误。我的错误说:

'object' does not contain a definition for 'Title'

我做错了什么?

2 个答案:

答案 0 :(得分:2)

那是因为你的代码的这一部分正在选择一个匿名对象。视图不知道是什么类型的对象。您的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>
}

或者更好的是,通过将以下内容放在页面顶部来创建强类型视图

 @model IEnumerable<Post>

在您的控制器中,您将返回      视图(GetBlogPosts());

在你看来你做了

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

答案 1 :(得分:0)

您可以使用部分视图而不是使用ViewBag,因为ViewBag。您需要在控制器中创建新的Action,然后使用HTML.RenderAction从Parent视图调用该操作,如下所示 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-现在使用(帖子)类型的强类型视图创建一个部分视图,并为其指定任何名称,然后像稍微更改一样编写代码。

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

3-现在在您的父视图中添加HTML.RenderAction屁股

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

有关详细信息,请查看以下链接

ChildActionExtensions.RenderAction Method

How to Use the ASP.NET MVC Render Action Helpers

Strongly Typed Views in MVC

RenderPartial vs RenderAction vs Partial vs Action in MVC Razor

When to use ViewBag, ViewData, or TempData

我希望有帮助