C# 如何将前端的json属性分配给API中的多个后端模型?
我有一个React组件,它可以像这样发布到API控制器:C# 如何将前端的json属性分配给API中的多个后端模型?,c#,asp.net-core-mvc,asp.net-core-webapi,C#,Asp.net Core Mvc,Asp.net Core Webapi,我有一个React组件,它可以像这样发布到API控制器: onSubmit={async values => { await new Promise(resolve => setTimeout(resolve, 500)); axios({ method: "POST", url: "/educationalgames/api/acceptentry", data: values }); alert(JS
onSubmit={async values => {
await new Promise(resolve => setTimeout(resolve, 500));
axios({
method: "POST",
url: "/educationalgames/api/acceptentry",
data: values
});
alert(JSON.stringify(values, null, 2));
}}
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
当我点击submit时,我看到它找到的值:
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
上面的数据是我的c#后端API中不同模型的混合属性
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
团队名称、团队电子邮件和培训师属于my team.cs模型
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
可删除层,部门属于department.cs模型
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
researchType、numOfStudents和currentState属于我的research.cs模型
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
我的问题是,我不知道如何转换数据,以便我的API控制器能够读取数据并为匹配的模型属性分配适当的值
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
到目前为止,我的c#controller中有一个:
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
[HttpPost]
public async Task<ActionResult> AcceptEntry([FromBody] ???)
[HttpPost]
public async Task<ActionResult> AcceptEntry([FromBody] CommonModel model) {
var team = new Team() {
teamName = model.teamName,
...
}
var department = new Department() {
eligiblePlayers = model.eligiblePlayers,
...
}
}
[HttpPost]
公共异步任务AcceptEntry([FromBody]??)
我有点不知所措,无法处理这件事
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
有办法吗
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
谢谢
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
teamName、teamEmail和trainer属于我的team.cs模型
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
eligiblePlayers、department属于department.cs模型
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
research Type、numOfStudents和currentState属于my research.cs
模型
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
为什么不在应用程序中创建通用模型
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
public class CommonModel {
public string teamName {get; set;}
teamEmail,
eligiblePlayers,
department,
researchType,
numOfStudents,
currentState
}
然后,在控制器中:
{
"eligiblePlayers": [],
"teamName": "FalconOne",
"teamEmail": "fc@coma.edu",
"trainer": "",
"department": "Physics",
"researchType": "Meta",
"numOfStudents": 50,
"currentState": true
}
[HttpPost]
public async Task<ActionResult> AcceptEntry([FromBody] ???)
[HttpPost]
public async Task<ActionResult> AcceptEntry([FromBody] CommonModel model) {
var team = new Team() {
teamName = model.teamName,
...
}
var department = new Department() {
eligiblePlayers = model.eligiblePlayers,
...
}
}
[HttpPost]
公共异步任务AcceptEntry([FromBody]CommonModel){
var团队=新团队(){
teamName=model.teamName,
...
}
var部门=新部门(){
eligiblePlayers=model.eligiblePlayers,
...
}
}
您可以将所有模型包装在一个单独的视图模型中,如图所示:只需为API创建一个新模型即可。您的模型不一定直接与后端模型相关。@DavidG但是如果前端的属性与我的模型不匹配,它们最终如何写入数据库?谢谢就像我说的,你的api模型不一定和你在数据库中使用的相同。在两者之间写一层,在两者之间转换。或者使用像AutoMapper这样的库。哦,我明白了,那么帖子会自动将值放入CommonModel模型吗?只有当React中的值对象与后端的CommonModel类的结构相同时。可能您的values对象是一个包含所有枚举参数的json。Net核心将接收到的json映射到您的类中(本例中为CommonModel)