模型绑定产生一个空列表

时间:2019-10-11 11:03:45

标签: c# .net-core model

当我尝试通过邮递员发送发帖请求时,产生的FROM mcr.microsoft.com/dotnet/core/sdk:2.1 AS build-env WORKDIR /app COPY src/MyAPI/*.csproj ./ RUN dotnet restore COPY src/MyAPI/ ./ RUN dotnet publish -c Release -o out FROM mcr.microsoft.com/dotnet/core/aspnet:2.1 WORKDIR /app COPY --from=build-env /app/out . ENTRYPOINT ["dotnet", "MyAPI.dll"] 列表为空。我在做什么错了?

这就是我作为TestCases

的表单参数传递的内容

TestCases

但是创建的结果模型具有[{"input_params":[{"value":"Hello World","type":"string"}],"expected_output":"test123123"}, {"input_params":[{"value":"Hello World2","type":"string"}],"expected_output":"test123123"}]的空列表

enter image description here

如何复制:

邮递员请求:

TestCases

控制器方法:

    {
    "info": {
        "_postman_id": "6f252c2d-adb0-487e-ba68-d32172ebae91",
        "name": "test",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
    },
    "item": [
        {
            "name": "Create Assignment",
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "",
                        "value": "application/json",
                        "type": "text",
                        "disabled": true
                    },
                    {
                        "key": "Content-Type",
                        "name": "Content-Type",
                        "value": "application/json",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "urlencoded",
                    "urlencoded": [
                        {
                            "key": "description",
                            "value": "testDesc2",
                            "type": "text"
                        },
                        {
                            "key": "test_cases",
                            "value": "[{\"input_params\":[{\"value\":\"Hello World\",\"type\":\"string\"}],\"expected_output\":\"test123123\"}, {\"input_params\":[{\"value\":\"Hello World\",\"type\":\"string\"}],\"expected_output\":\"test123123\"}]",
                            "type": "text"
                        },
                        {
                            "key": "TestCases",
                            "value": "TestCases[]=1050&TestCases[]=2000\n",
                            "type": "text",
                            "disabled": true
                        }
                    ]
                },
                "url": {
                    "raw": "{{localhost}}/api/assignment/create",
                    "host": [
                        "{{localhost}}"
                    ],
                    "path": [
                        "api",
                        "assignment",
                        "create"
                    ]
                }
            },
            "response": []
        }
    ],
    "protocolProfileBehavior": {}
}

型号:

 [HttpPost("create")]
    public async Task<ActionResult<Assignment>> CreateAssignment([FromForm] Assignment assignment)
    {
        Debug.Write(assignment);
        return Ok();
    }

TestCase模型:

 public class Assignment : EntityBase
{
    public Assignment()
    {
        this.CreatedDate = DateTime.UtcNow;
    }

    [Required]
    public string Description { get; set; }

    [JsonProperty("test_cases")]
    [BindProperty(Name = "test_cases")]
    [Required]
    public virtual List<TestCase> TestCases { get; set; }

    public virtual List<Solution> Solutions { get; set; }

    [DataType(DataType.Date)]
    [JsonProperty("created_date")]
    [Required]
    public DateTime CreatedDate { get; private set; }
}

EntityBase:

 public class TestCase : EntityBase
{
    [JsonProperty("input_params")]
    [BindProperty(Name = "input_params")]
    public virtual List<InputParam> InputParams { get; set; }

    [JsonProperty("expected_output")]
    [BindProperty(Name = "expected_output")]
    public string ExpectedOuput { get; set; }

    [JsonIgnore]
    public int AssignmentId { get; private set; }
    [JsonIgnore]
    public virtual Assignment Assignment { get; private set; }
}

0 个答案:

没有答案