簡體   English   中英

如何從Initiliaze獲取列表? C#

[英]How to get the List from Initiliaze? C#

我有2個模型-帖子,評論

在后期模型上,我做:

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

在我創建SampleData模型進行初始化並且在Views上獲得index.cshtml之后,我可以得到我的第一條Post,但是當我嘗試獲取Post.Comments時我得到的是NULL,並且在初始化時一切正常。

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

要初始化,您必須使用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"
                    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM