Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.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# 添加到模型';在ASP.NETCore3.0中使用表单创建IList_C#_Asp.net_.net_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# 添加到模型';在ASP.NETCore3.0中使用表单创建IList

C# 添加到模型';在ASP.NETCore3.0中使用表单创建IList,c#,asp.net,.net,asp.net-mvc,asp.net-core,C#,Asp.net,.net,Asp.net Mvc,Asp.net Core,在尝试学习ASP.NETCore时,我决定制作一个论坛将是一个很好的学习项目 因此,我创建了以下模型: public class Topic { public int Id {get; set;} public int PostCount {get; set;} public string Title {get; set;} public string OriginalPoster {get; set;} public IList<Post>

在尝试学习ASP.NETCore时,我决定制作一个论坛将是一个很好的学习项目

因此,我创建了以下模型:

public class Topic
{
    public int Id {get; set;}
    public int PostCount {get; set;}
    public string Title {get; set;}
    public string OriginalPoster {get; set;}
    public IList<Post> Posts{get; set;}
}

public class Post
{
    public int Id {get; set;}
    public DateTime TimeStamp {get; set;}
    public string Poster {get; set;}
    public string Body {get; set;}
}
任何和所有的帮助都将不胜感激

主题的MySQL表如下所示:

+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| Id             | varchar(20) | YES  |     | NULL    |       |
| PostCount      | int(11)     | YES  |     | NULL    |       |
| Title          | varchar(20) | YES  |     | NULL    |       |
| OriginalPoster | varchar(20) | YES  |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+
至于职位:

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| Id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| TimeStamp | datetime(6) | NO   |     | NULL    |                |
| Poster    | longtext    | YES  |     | NULL    |                |
| Body      | longtext    | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

好的,在我们在评论中的对话之后,我将尝试一个答案

首先,您需要对数据库表进行一些更改

  • 主题
    表上的ID应自动递增

  • 您的
    Post
    表中需要有一个与
    主题
    表相关的外键。没有这些,我们就无法知道
    Post
    a
    Topic
    属于哪个主题。如果操作正确,则该关系实际上应该在数据库中定义。换句话说,不要只添加FK列。采取额外的步骤告诉SQL它与父表相关。你可以用谷歌搜索如何做到这一点。例子不胜枚举

  • 我真的不会给你的ID列命名
    ID
    。让他们有唯一的名字<代码>PostId和
    主题ID
    。这会帮你省去很多麻烦

  • 现在,让我们看看代码中的模型。在代码中建立关系的方法是在

    Post
    模型中定义外键。在您的情况下,看起来是这样的:

    public class Topic
    {
        [Key]
        public int TopicId {get; set;}
        public int PostCount {get; set;}
        public string Title {get; set;}
        public string OriginalPoster {get; set;}
        public IList<Post> Posts{get; set;}
    }
    
    public class Post
    {
        [Key]
        public int PostId {get; set;}
        ForeignKey("Topic")]
        public int TopicId{get; set;}
        public DateTime TimeStamp {get; set;}
        public string Poster {get; set;}
        public string Body {get; set;}
    }
    
    公共类主题
    {
    [关键]
    公共int-TopicId{get;set;}
    公共整数后计数{get;set;}
    公共字符串标题{get;set;}
    公共字符串OriginalPoster{get;set;}
    公共IList Posts{get;set;}
    }
    公营职位
    {
    [关键]
    公共int PostId{get;set;}
    外键(“主题”)]
    公共int-TopicId{get;set;}
    公共日期时间时间戳{get;set;}
    公共字符串海报{get;set;}
    公共字符串体{get;set;}
    }
    
    现在,EF将了解这两个类之间的关系,以及它们之间的关系

    我认为这应该足以让代码正常工作。但是如果你遇到任何其他问题,要么在下面发表评论,要么开始一个新问题(并在下面发表评论并链接到新问题)

    希望这有帮助

    理由
  • 我认为您实际上并没有写入数据库,您的主题模型中的IList字段在数据库中并不存在,因此您无法插入它
  • 解决方案
  • 数据库设计中存在一些问题。这是一个典型的一对多数据集问题。您需要将这两个表与外键相关联,以便可以根据键值更新post表
  • 您的数据库表应该修改为如下所示
  • 我复制了你的代码并把它放在GitHub上。您可以单击此处查看如何实现插入操作。这是插入零件的代码
  • [HttpPost]
    公共异步任务CreateTopic(TopicViewModel模型)
    {
    var topic=新主题
    {
    Title=model.Title,
    OriginalPoster=model.OriginalPoster,
    PostCount=model.PostCount
    };
    wait_context.AddAsync(主题);
    wait_context.SaveChangesAsync();
    返回重定向到操作(名称(索引));
    }
    [HttpPost]
    公共异步任务CreatePost(PostViewModel模型)
    {
    var post=新职位
    {
    TopicId=model.TopicId,
    时间戳=model.TimeStamp,
    海报=模型。海报,
    Body=model.Body
    };
    wait_context.AddAsync(post);
    wait_context.SaveChangesAsync();
    返回重定向到操作(名称(索引));
    }
    
  • 通过以上描述和代码应该能够解决您当前的问题
  • 其他提示
  • 您对.NETCore中模型的理解存在一些偏差。模型可以分为两种类型:映射数据库实体和为前端页面工作提供服务。相关的
  • 的文件。Net内核不是很全面。可以找到许多小知识

  • 您是否曾经尝试过将
    IList
    更改为类似
    Post[]
    的数组?“它向我表明这不起作用”--您可以通过提供更多细节来帮助我们吗?有错误吗?还是不是你所期望的行为?“不工作”是什么意思?我已经更新了我的问题,以便更能描述我看到的错误。好的,谢谢。这很有帮助。我怀疑这里有很多问题需要解决。首先,让我们这样做。。。当您在
    Create(topictops)
    方法中点击断点,并将鼠标悬停在
    tops
    上时,该对象看起来是什么样子?它拥有你期望它拥有的一切吗?@CaseyCrookston它拥有是的,我期望为空的东西是空的,我期望为非空的东西不是空的。具体来说,Posts的长度为1,其主体由我在表单中创建的消息组成,并且时间戳也正在生成。Posts中Post的Id是0,我认为这表明它被分配了数据库中的第0个Id。
    +-----------+-------------+------+-----+---------+----------------+
    | Field     | Type        | Null | Key | Default | Extra          |
    +-----------+-------------+------+-----+---------+----------------+
    | Id        | int(11)     | NO   | PRI | NULL    | auto_increment |
    | TimeStamp | datetime(6) | NO   |     | NULL    |                |
    | Poster    | longtext    | YES  |     | NULL    |                |
    | Body      | longtext    | YES  |     | NULL    |                |
    +-----------+-------------+------+-----+---------+----------------+
    
    public class Topic
    {
        [Key]
        public int TopicId {get; set;}
        public int PostCount {get; set;}
        public string Title {get; set;}
        public string OriginalPoster {get; set;}
        public IList<Post> Posts{get; set;}
    }
    
    public class Post
    {
        [Key]
        public int PostId {get; set;}
        ForeignKey("Topic")]
        public int TopicId{get; set;}
        public DateTime TimeStamp {get; set;}
        public string Poster {get; set;}
        public string Body {get; set;}
    }
    
        public class Topic
        {
            [ScaffoldColumn(false)]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int TopicId { get; set; }
            public int PostCount { get; set; }
            public string Title { get; set; }
            public string OriginalPoster { get; set; }
        }
    
        public class Post
        {
            [ScaffoldColumn(false)]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int PostId { get; set; }
            public int TopicId { get; set; }
            [ForeignKey("TopicId")]
            public DateTime TimeStamp { get; set; }
            public string Poster { get; set; }
            public string Body { get; set; }
        }
    
            [HttpPost]
            public async Task<IActionResult> CreateTopic(TopicViewModel model)
            {
                var topic = new Topic
                {
                    Title = model.Title,
                    OriginalPoster = model.OriginalPoster,
                    PostCount = model.PostCount
                };
                await _context.AddAsync(topic);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            [HttpPost]
            public async Task<IActionResult> CreatePost(PostViewModel model)
            {
                var post = new Post
                {
                    TopicId = model.TopicId,
                    TimeStamp = model.TimeStamp,
                    Poster = model.Poster,
                    Body = model.Body
                };
                await _context.AddAsync(post);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }