Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC在视图中的foreach循环中显示相关数据_C#_Linq_Asp.net Mvc 5_Viewmodel - Fatal编程技术网

C# MVC在视图中的foreach循环中显示相关数据

C# MVC在视图中的foreach循环中显示相关数据,c#,linq,asp.net-mvc-5,viewmodel,C#,Linq,Asp.net Mvc 5,Viewmodel,我创建了一个Posts,Comments表,并希望以与本教程类似的方式显示相关数据。我是linq和MVC5的新手 我想显示文章标题,点击它会显示文章的详细信息和他们的评论 根据我在下面的尝试,我只能获得要显示的post表信息,我无法显示任何评论,只是在我放置item.comments.Comment\u Content,item.Comment时出错 这是我的视图模型 using System; using System.Collections.Generic; using System.Lin

我创建了一个Posts,Comments表,并希望以与本教程类似的方式显示相关数据。我是linq和MVC5的新手

我想显示文章标题,点击它会显示文章的详细信息和他们的评论

根据我在下面的尝试,我只能获得要显示的post表信息,我无法显示任何评论,只是在我放置item.comments.Comment\u Content,item.Comment时出错

这是我的视图模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; 
using Roe8_2.Models;

namespace Roe8_2.ViewModels
{
    public class PostIndexData
    {
        public IEnumerable<Post> Posts { get; set; }
        public IEnumerable<Comment> Comments { get; set; }
        public IEnumerable<Person> Persons { get; set; } 


    }
}
这是一种观点

@model  Roe8_2.ViewModels.PostIndexData 

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    @foreach (var item in Model.Posts)
    {
        string selectedRow = "";
        if (item.PostID == ViewBag.PostID)
        {
            selectedRow = "success";
        }
        <tr class="@selectedRow">
            <td>
                @Html.DisplayFor(modelItem => item.Title)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Content)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PostDate)
            </td>
            <td>
                @if (item.Comment != null)
                    {
                        @item.Comment.Comment_Content;
                    }

                </td>
                <td>
                    @Html.ActionLink("Select", "Index", new { id = item.PostID }) |
                    @Html.ActionLink("Edit", "Edit", new { id = item.PostID }) |
                    @Html.ActionLink("Details", "Details", new { id = item.PostID }) |
                    @Html.ActionLink("Delete", "Delete", new { id = item.PostID })
                </td>
                </tr>
    }

</table>
@model Roe8_2.ViewModels.PostIndexData
@{
ViewBag.Title=“Index”;
}
指数

@ActionLink(“新建”、“创建”)

@foreach(Model.Posts中的var项) { 字符串selectedRow=“”; 如果(item.PostID==ViewBag.PostID) { selectedRow=“成功”; } @DisplayFor(modeleItem=>item.Title) @DisplayFor(modelItem=>item.Content) @DisplayFor(modelItem=>item.PostDate) @如果(item.Comment!=null) { @item.Comment.Comment_内容; } @ActionLink(“选择”,“索引”,新建{id=item.PostID})| @ActionLink(“编辑”,“编辑”,新的{id=item.PostID})| @ActionLink(“详细信息”,“详细信息”,新的{id=item.PostID})| @ActionLink(“删除”,“删除”,新的{id=item.PostID}) }
这些是桌子

    public class Post
    {
        public int PostID { get; set; }
        public int PersonID { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        [DataType(DataType.Date)] 
        public DateTime PostDate { get; set; }

        public virtual ICollection<Comment> Comment { get; set; }
        public virtual Person Person { get; set; }
    }

  public class Comment
    {
        public int CommentID { get; set; }
        public int PostID { get; set; }
        public int PersonID { get; set; }
        public string Comment_Content { get; set; }
        public DateTime CommentDate { get; set; }
        public virtual Post Post { get; set; }

        public virtual Person Person { get; set; } 
    }
}

    public class Person
    {
        public int ID { get; set; }

        [Required]
        [StringLength(128)]
        public string AspNetUsersID { get; set; }
        [StringLength(50, ErrorMessage = "Last name cannot be longer than 50 characters.")]
        public string LastName { get; set; }
        [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
        public string FirstName { get; set; }

        public DateTime RegDate { get; set; }
        public string FullName
        {
            get
            {
                return LastName + ", " + FirstName;
            }
        }
        public virtual AspNetUser AspNetUsers { get; set; }

    }
公共类职位
{
公共int PostID{get;set;}
公共int PersonID{get;set;}
公共字符串标题{get;set;}
公共字符串内容{get;set;}
[数据类型(DataType.Date)]
public DateTime PostDate{get;set;}
公共虚拟ICollection注释{get;set;}
公共虚拟人{get;set;}
}
公开课评论
{
public int CommentID{get;set;}
公共int PostID{get;set;}
公共int PersonID{get;set;}
公共字符串注释\u内容{get;set;}
public DateTime CommentDate{get;set;}
公共虚拟Post{get;set;}
公共虚拟人{get;set;}
}
}
公共阶层人士
{
公共int ID{get;set;}
[必需]
[第128段]
公共字符串AspNetUsersID{get;set;}
[StringLength(50,ErrorMessage=“姓氏不能超过50个字符。”)]
公共字符串LastName{get;set;}
[StringLength(50,ErrorMessage=“名字不能超过50个字符。”)]
公共字符串名{get;set;}
公共日期时间RegDate{get;set;}
公共字符串全名
{
得到
{
返回LastName+“,”+FirstName;
}
}
公共虚拟AspNetUser AspNetUsers{get;set;}
}

调用索引控制器时,如何填充commentId?在检查Id是否为空之前,先填充帖子。在检查commentId是否为null之前,您没有填充注释。您收到了什么错误?当我插入以下代码时,我得到了以下错误@如果(item.Comment!=null){@item.Comment.Comment\u Content;}CS1061:“ICollection”不包含“Comment\u Content”的定义,并且找不到接受类型为“ICollection”的第一个参数的扩展方法“Comment\u Content”(您是否缺少using指令或程序集引用?)
    public class Post
    {
        public int PostID { get; set; }
        public int PersonID { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        [DataType(DataType.Date)] 
        public DateTime PostDate { get; set; }

        public virtual ICollection<Comment> Comment { get; set; }
        public virtual Person Person { get; set; }
    }

  public class Comment
    {
        public int CommentID { get; set; }
        public int PostID { get; set; }
        public int PersonID { get; set; }
        public string Comment_Content { get; set; }
        public DateTime CommentDate { get; set; }
        public virtual Post Post { get; set; }

        public virtual Person Person { get; set; } 
    }
}

    public class Person
    {
        public int ID { get; set; }

        [Required]
        [StringLength(128)]
        public string AspNetUsersID { get; set; }
        [StringLength(50, ErrorMessage = "Last name cannot be longer than 50 characters.")]
        public string LastName { get; set; }
        [StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
        public string FirstName { get; set; }

        public DateTime RegDate { get; set; }
        public string FullName
        {
            get
            {
                return LastName + ", " + FirstName;
            }
        }
        public virtual AspNetUser AspNetUsers { get; set; }

    }