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

时间:2017-01-25 03:26:10

标签: c# linq asp.net-mvc-5 viewmodel

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

我想显示帖子标题,点击后会显示帖子详细信息及其评论。

从我在下面尝试的内容中,我只能显示帖子表信息,我无法显示任何评论,只是当我把item.Comments.Comment_Content,item.Comment无法正常工作时出错。

这是我的viewmodel;

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; } 


    }
}

这是控制器

        public ActionResult Index(int? id, int? CommentID)
    {
        var viewModel = new PostIndexData();

        viewModel.Posts = db.Posts
            .Include(p => p.Comment) 
            .OrderByDescending(p => p.PostDate);

        if (id != null)
        {
            ViewBag.PostID = id.Value;
            viewModel.Comments = viewModel.Posts.Where(p => p.PostID == id.Value).SingleOrDefault().Comment;
        }
        if (CommentID != null)
        { 
            ViewBag.PostID = id.Value;
            viewModel.Comments = viewModel.Comments.Where(p => p.PostID == id.Value);
}
            return View(viewModel);
        }

这是观点;

@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>

这些是表格;

    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; }

    }

0 个答案:

没有答案