【问题标题】:Displaying results from LINQ query in ASP.NET MVC在 ASP.NET MVC 中显示 LINQ 查询的结果
【发布时间】:2014-02-02 19:28:27
【问题描述】:

我有一个包含 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'

我做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc linq


    【解决方案1】:

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

    【讨论】:

    • “发布”定义应该去哪里?我在模型文件夹中创建了一个类。但是,我的 .cshtml 文件仍然找不到它。我收到一条错误消息:“CS0246:类型或命名空间名称“帖子”找不到..."
    • 您可以只使用从 GetBlogPosts() 返回的完整命名空间和类型名称,例如,如果您的 GetBlogPosts() 返回 List 然后将 @Model List
    【解决方案2】:

    您可以使用 部分视图 而不是使用 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>
    }
    

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

        <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

    希望能帮到你

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多