【问题标题】:How to get the List from Initiliaze? C#如何从初始化中获取列表? C#
【发布时间】:2016-05-11 07:00:37
【问题描述】:

我有 2 个模型 - 帖子、评论

关于我制作的后期模型:

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

在我创建 SampleData 模型进行初始化之后 在 Views 我得到 index.cshtml ,我可以得到我的第一个 Post 但是当我尝试获取 Post.Comments 时,我得到了 NULL ,并且在 Initialize all 上运行良好..

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

【问题讨论】:

    标签: c# object get


    【解决方案1】:

    要初始化你必须使用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"
                        }
    };
    

    【讨论】:

    • 谢谢,用新列表解决了这个问题.. 但是在“foreach”上从列表中获取对象,我得到 0 个对象而不是 3 个对象..
    • 我已经更新了答案,如果有帮助你可以接受这个答案
    • 不工作,我需要将它们添加到上下文+列表中
    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多