如何从Initiliaze获取列表? C#

时间:2016-05-11 07:00:37

标签: c# object get

我有两个模特 - 帖子,评论

在帖子模型上我做:

public List<Comment> Comments { get; set; }

在我创建SampleData模型到Initialize之后 在视图上我得到了index.cshtml,我可以得到我的第一个帖子 但是当我试图获得Post.Comments时,我得到NULL,并且在Initialize上运行良好..

SampleData.cs:

    using System;
    using System.Linq;
    using Microsoft.Data.Entity;
    using Microsoft.Extensions.DependencyInjection;
    using System.Collections.Generic;

    namespace WebShauli.Models
    {
        public class SampleData
        {
            public static void Initialize(IServiceProvider serviceProvider)
            {
            var context = serviceProvider.GetService<ApplicationDbContext>();
            {
              context.Database.Migrate();
              if (!context.Comment.Any())
              {

                var Post1 = context.Post.Add(
                    new Post { Author = "Elad", AuthorURL = "http://www.momlazim.com", Content = "Hello everybody , this is my first post", Date = new DateTime(2016, 05, 09), Comments = context.Comment.ToList<Comment>(), Title = "First post", Image = "blabla", Video = "blabla2" }).Entity;
                Post1.Comments = new List<Comment>();
                context.Comment.AddRange(
                    new Comment()
                    {
                        Post = Post1,
                        Title = "Comment 3",
                        Content = "Hello , this is the first comment",
                        WriterComment = "Eli",
                        WriterURL = "http://www.ynet.co.il"
                    },
                    new Comment()
                    {
                        Post = Post1,
                        Title = "Comment 2",
                        Content = "Hello , this is the first comment",
                        WriterComment = "Efffli",
                        WriterURL = "http://www.ynet.co.il"
                    },
                    new Comment()
                    {
                        Post = Post1,
                        Title = "Comment 1",
                        Content = "Hello , this is the first comment",
                        WriterComment = "Efffli",
                        WriterURL = "http://www.ynet.co.il"
                    }
                );
                context.SaveChanges();
            }
        }
     }
  }
}

index.cs:

@model IEnumerable<WebShauli.Models.Post>
@{
ViewData["Title"] = "Blog";
}

<form action="#">
<h3>Search post</h3>
Date between: <input id="inputDateStart" type="date" name="startDate"> to      <input id="inputDateEnd" type="date" name="endDate">
<br />
Author name: <input id="inputAuthor" type="text" name="Full name">
<br />
E-mail: <input id="inputEmail" type="email" name="email">
<br />
Words from posts: <input type="text" name="wordFromPosts">
<br />
Minimum replys for post: <input type="range" name="points" min="0" max="10">
<br />
<input type="submit" value="Search">
</form>

@foreach (var item in Model)

{
<section>

    <article class="blogPost">
        <header>
            <h2>@Html.DisplayFor(Model => item.Title)</h2>
            <p>Posted on <time datetime="@Html.DisplayFor(Model => item.Date)">@Html.DisplayFor(Model => item.Date)</time> by <a href="@Html.DisplayFor(Model => item.AuthorURL)">@Html.DisplayFor(Model => item.Author)</a> - <a href="@Html.DisplayFor(Model => item.PostID)">@Html.DisplayFor(Model => item.Comments.Count) comments</a></p>
        </header>
        <div>
            <p>@Html.DisplayFor(Model => item.Content)</p>

            <img src="@Html.DisplayFor(Model => item.Image)" alt="picture" />
            <p></p>
            <video controls="controls">
                <source src="@Html.DisplayFor(Model => item.Video)" type="video/mp4" />
                Your browser does not support the video tag.
            </video>
            <p></p>
        </div>
    </article>
</section>
<section id="@Html.DisplayFor(Model => item.PostID)">
    <h3>Comments</h3>
    @foreach (var itemC in item.Comments)
    {
        <article>
            <header>
                <a href="@Html.DisplayFor(Model => itemC.WriterURL)">@Html.DisplayFor(Model => itemC.WriterComment)</a>
            </header>
            <p>@Html.DisplayFor(Model => itemC.Content)</p>
        </article>
    }
</section>
@using (Html.BeginForm("AddComment", "Blog"))
{ 
    @Html.AntiForgeryToken()
    <h3>Post a comment</h3>
    @Html.Hidden("PostID",item.PostID)
    <p>
        <label for="Name">Name</label>
        @Html.TextBox("WriterComment")
    </p>
    <p>
        <label for="Website">Website</label>
        @Html.TextBox("WriterURL")
    </p>
    <p>
        <label for="Title">Title</label>
        @Html.TextBox("Title")
    </p>
    <p>
        <label for="Comment">Comment</label>
        @Html.TextBox("Content")
    </p>
    <p><input type="submit" value="Post comment" /></p>
    }
}

1 个答案:

答案 0 :(得分:2)

初始化你必须使用new并在你的代码中创建了引用,但没有在内存中创建列表,因此引用实际上没有指向任何内容。所以要初始化新列表

public List<Comment> comments = new List<Comment>();

并将项目添加到列表

public List<Comment> comments = new List<Comment>
{
                    new Comment
                    {
                        Post = Post1,
                        Title = "Comment 3",
                        Content = "Hello , this is the first comment",
                        WriterComment = "Eli",
                        WriterURL = "http://www.ynet.co.il"
                    },
                    new Comment
                    {
                        Post = Post1,
                        Title = "Comment 2",
                        Content = "Hello , this is the first comment",
                        WriterComment = "Efffli",
                        WriterURL = "http://www.ynet.co.il"
                    },
                    new Comment
                    {
                        Post = Post1,
                        Title = "Comment 1",
                        Content = "Hello , this is the first comment",
                        WriterComment = "Efffli",
                        WriterURL = "http://www.ynet.co.il"
                    }
};