将属性绑定到嵌套数组属性中的另一个输入名称

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

Bind property to another input name in nested array property

问题

以下是翻译好的部分:

The data in JSON format is sent to api endpoint.

Data sample:

{
"templateId": "dc15e4d1-ccbd-4581-a819-5b7f90b32cc5",
"name": "abc",
"steps": [
{
"id": "34b4f406-120e-4d80-8018-6c780c80a6c4",
"visible": false
}
]
}

Api gets data in this format:

public class TemplateRequest {
public Guid TemplateId { get; set; }

    public string Name { get; set; }

    public StepRequest[] Steps { get; set; }

}

StepRequest class:

public class StepRequest {
[ModelBinder(Name = "id")]
public Guid StepId { get; set; }

    public bool? Visible { get; set; }
}

The JSON has id key instead of stepId, but I can't get it in controller.

When I check, the StepId is always an empty Guid.

What is wrong here, why the StepId property is not having the value from id key?

英文:

The data in JSON format is sent to api endpoint.

Data sample:

    {
      "templateId": "dc15e4d1-ccbd-4581-a819-5b7f90b32cc5",
      "name": "abc",
      "steps": [
        {
          "id": "34b4f406-120e-4d80-8018-6c780c80a6c4",
          "visible": false,          
        }
      ]
    }

Api gets data in this format:

public class TemplateRequest {
        public Guid TemplateId { get; set; }

        public string Name { get; set; }

        public StepRequest[] Steps { get; set; }
}

StepRequest class:

public class StepRequest {
        [ModelBinder(Name = "id")]
        public Guid StepId { get; set; }

        public bool? Visible { get; set; }
    }

The JSON has id key instead of stepId, but I can't get it in controller.

When I check, the StepId is always an empty Guid.

What is wrong here, why the StepId property is not having the value from id key?

答案1

得分: 0

是的,就像 @daremachine 说的那样,这是NewtonSoft和 JsonProperty(PropertyName = "id") 有帮助。

英文:

Yes, like @daremachine said, it was NewtonSoft and JsonProperty(PropertyName = "id") helped.

huangapple
  • 本文由 发表于 2023年2月16日 08:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466715.html
匿名

发表评论

匿名网友

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

确定