【发布时间】:2021-06-30 22:13:57
【问题描述】:
我尝试将两个模型绑定到一个模型中。我的代码没有任何错误,但是当我尝试使用邮递员发布数据时出现错误。
这里,我的第一个模型,我绑定两个模型的模型
public class CModel
{
[Key]
public int id { get; set; }
public AModel amodelid { get; set; }
public BModel bmodelid { get; set; }
}
我的第二个模型
public class AModel
{
[Key]
public int id { get; set; }
.
.
.
}
我的第三个模型
public class BModel
{
[Key]
public int id { get; set; }
.
.
.
}
我尝试在 CModel 中获取两个模型 ID 但是当我尝试从邮递员那里发送一个帖子请求时,我收到了这个错误
{
"errors": {
"amodelid": [
"Error converting value 2 to type 'Models.AModel'. Path 'amodelid', line 2, position 17."
],
"bmodelid": [
"Error converting value 1 to type 'Models.BModel'. Path 'bmodelid', line 3, position 18."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
}
我和邮递员一起发送的 JSON
{
"amodelid":2,
"bmodelid":1
}
以及来自控制器的最后一个代码引用
[HttpPost("/api/test/")]
public async Task<ActionResult> SomeTask([FromBody] CModel cModel)
{
//result return bool
var result = await _someService.MoreSomeTask(cModel).ConfigureAwait(false);
if (result)
{
return Ok("OK");
}
return BadRequest(new { msg = "Please check your request" });
}
我只发送控制器的代码,因为即使请求也没有到达我的控制器(我尝试在调试模式下运行程序,所以我得到了这个结果)。另外,我在 startup.cs 文件中注册了我的服务。 抱歉,我的问题有点长,但我会尽可能多地解释。 感谢您的帮助。
【问题讨论】:
标签: .net asp.net-core asp.net-web-api