简体   繁体   English

CRUD一对一关系插入数据

[英]CRUD one-to-one relationship inserting data

I have a really simple two tables made with code-first approach with relationship one to one. 我有一个非常简单的两张表,它们是用代码优先的方法与一对一的关系制成的。 I do not know what is the best way to insert data. 我不知道什么是插入数据的最佳方法。

Since each trip has one coach should I have two CRUD pages one for trips one for coaches? 既然每次旅行都有一位教练,我是否应该有两页CRUD页面用于旅行,一页一页用于教练? But then would I need another page to link them up together? 但是,我是否需要另一个页面将它们链接在一起?

What I'm asking is: is there a way to make on one CRUD page eg when creating a trip to add a coach to it. 我要问的是:是否有一种方法可以在一个CRUD页面上进行创建,例如,在创建行程以向其添加教练时。

public class Coach
    {
        public int CoachID { get; set; }
        public int Capacity { get; set; }

        [Required]
        public Trip Trip { get; set; }
    }

public class Trip
    {
        public int TripID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }

        public List<Ticket> Tickets { get; set; }
        public Coach Coach { get; set; }

    }

The issue that I'm facing right now is after adding trip I'm going to add a coach which has a dropdown to select for which trip do I want to add this coach and pressing create does nothing the page just stays the same nothing happens. 我现在面临的问题是添加行程后,我将添加一个教练,该教练具有一个下拉菜单以选择要添加该教练的行程,然后按“创建”不执行任何操作,页面保持不变。 增加行程 添加教练

I would usually create method that are simple, does only one thing. 我通常会创建简单的方法,只做一件事。

I would create two simple CRUD for both and have another service method that calls both 'Create' for Coach and 'Create' for Trip. 我将为两者创建两个简单的CRUD,并具有另一个服务方法,该方法将Coach称为“ Create”,而将Trip称为“ Create”。

Make sure that each form is actually wrapped in a form tag and that your buttons type is "submit". 确保每个表单实际上都包裹在一个表单标签中,并且您的按钮类型为“提交”。 This should at least get you to your next step of figuring out how you would like to implement it. 这至少应该使您下一步了解如何实现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM