如何将数据保存到2个表格中

huangapple go评论64阅读模式
英文:

How to save data into 2 tables

问题

I need to save my question module into two tables. The first one is the "question" table, which contains questionId, question category from the category table, question type from the question type table, question weightage, and question description. In the "question" table, there is also a "questionanswerId," question id, typeId, options, and points. I need to store these things from one form. I'm using .NET Web API for the backend and Angular for the front end. Can you please help me do this?

我需要将我的问题模块保存到两个表中。第一个表是"question"表,其中包含questionId、来自category表的question category、来自question type表的question type、question weightage和question description。在"question"表中,还有一个"questionanswerId"、question id、typeId、options和points。我需要从一个表单中存储这些数据。我正在使用.NET Web API作为后端和Angular作为前端。您能帮助我完成这个任务吗?

I need to save my data into 2 tables from one form using .NET and Angular.

我需要从一个表单中将数据保存到2个表中,使用.NET和Angular。

英文:

I need to save my question module into two tables. first one is question and second one is question answer table. question table contains questionId,question category from category table,question type from question type table, question weightage, question description. In the question table there is questionanswerId, question id, typeId, options ,points. I need to store these thimgs from one form. I'm using .net web api for backend and angular for front end. Can you please help me to do this?

i need save these data into two tables using .net and angular

I need to save my data into 2 tables from one form using .net amd angular

答案1

得分: 1

以下是已翻译的部分:

有多种方法可以实现这个,具体取决于您想要绘制分隔线的位置。在某一点上,添加问题和问题答案的命令将需要成为两个单独的命令。看起来您已经在单个API调用中向服务器发送了问题和答案。如果您想继续使用这种方法,只需将问题和答案分别添加到数据库中即可。您可能需要创建一个新对象,该对象具有 question 属性和 answer 属性,并在您的 AddQuestion 操作中接受它,因为在 .net 中使数据绑定器与单独的参数一起工作可能会很困难(我不确定您使用的是哪个版本)。一旦数据到达 .net 服务器,将分别进行数据库调用以将所有项目添加到各自的表中应该不会太难。

另一种解决方案是为添加答案创建一个单独的控制器操作。这将需要 Angular/Typescript 在提交后将表单拆分成问题部分和答案部分,然后使用正确的数据调用适当的控制器操作。您可能需要首先提交问题,然后等待来自 .net web 服务器的回复,以获取问题的ID以添加到答案中,以便它们可以关联起来。这还要求您将存储过程拆分为2个过程,一个用于问题,一个用于答案。

英文:

There are several ways to do it, depending on where you want to draw the separation line. At some point the command to add a question and a question answer are going to need to be 2 separate commands. It looks like you are sending the question and answer to the server in a single API call already. If you want to keep that approach, you just need to add the question and the answer to the database separately. You may need to create a new object which has a question property and a answer property, and accept that in your AddQuestion action since getting the data binder to work with separate parameters can be difficult in .net (I'm not sure which version you are using). Once the data gets to the .net server, it shouldn't be too hard to make separate database calls to add all the items to their various tables.

Another solution would be to have a separate controller action for adding an answer. This would require Angular/Typescript to split up the form, after submitting, into the question part and the answer part, then call the appropriate controller action with the correct data. You would probably need to submit the question first, then wait for the reply from the .net web server to get the question's id to add to the answer so they can be associated. This would also require you to split your stored procedure into 2 procedures, one for questions, one for answers.

huangapple
  • 本文由 发表于 2023年6月1日 00:41:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定