我有以下模型代码:
public partial class RequestMessage
{
[JsonProperty("Foo")]
public RequestMessageModel requestMessageModel { get; set; }
}
public partial class RequestMessageModel
{
[JsonProperty("id")]
[Required(ErrorMessage = "id is required")]
[FromRoute(Name = "id")]
public string id{ get; set; }
/// <summary>anotherID</summary>
[JsonProperty("anotherID"), Required(ErrorMessage = "anotherID is required")]
[FromRoute(Name = "anotherID")]
public string AnotherID{ get; set; }}
然后我有这个Controller方法:
public async Task<ActionResult<ResponseMessage>> GetByIdByAnotherIDAsync([FromRoute] RequestMessage reqMsg)
{ if (ModelState.IsValid && reqMsg.RequestMessageModel.IsValid()) return await GetMethodAsync(reqMsg);
else{
return this.BadRequest();
} }
我的问题是reqMsg一直为null。我在模型中使用部分类,可以“展平”它并可能使其工作,但是可以使用[FromRoute]将数据绑定到传入的模型类型的子模型中吗? / p>