asp.net-core-webapihttp-delete

Unable to convert JSON value to a list of class in HttpDelete response


Using .Net Core I develop a HttpDelete controller

[HttpDelete("dqc")]
[AllowAnonymous]
public async Task<IActionResult> DeleteDQCAsync([FromBody] IEnumerable<DqcDto> dqcs, CancellationToken cancellationToken)
{
    try
    {
        var response = await _managerService.DeleteDqcAsync(dqcs, cancellationToken);
        if (response)
        {
            return Ok($"DQC codes have been deleted successfully");
        }
        else
        {
            return BadRequest("Delete selected DQC codes failed.");
        }
    }
    catch (Exception)
    {
        throw;
    }
}

The DqcDto class is designed as:

public class DqcDto
{
    public string? DqcCode { get; set; }
    public string? DqcDescription { get; set; }
    public string? Project { get; set; }
    public string? ProjectGroup { get; set; }
    public string? Task { get; set; }
    public bool? IsCheck { get; set; }
}

When I test using Postman with payload in Body

{
    "dqcs": [
        {
            "DqcCode":"DQC-005-B-1",
            "DqcDescription":"Is the naming",
            "Project":"D1",
            "ProjectGroup":"D",
            "Task":"T1",
            "IsCheck":true
        },
        {
            "DqcCode":"DQC-109-1-1",
            "DqcDescription":"Checked-In",
            "Project":"D2",
            "ProjectGroup":"D",
            "Task":"T2",
            "IsCheck":true
        }
    ]
}

I get following error

"errors": {
        "$": [
            "The JSON value could not be converted to System.Collections.Generic.IEnumerable`1[IQC_Database_Management_API.Dtos.DqcDto]. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
        ],
        "dqcs": [
            "The dqcs field is required."
        ]
    }

I have checked that there is no spelling issue. I donot understand why it shows "dqcs" filed is required. I have assigned it in the Body. Could someone tell me where I went wrong?


Solution

  • Your controller calls an array, so your body payload should be

    [
        {
            "DqcCode":"DQC-005-B-1",
            "DqcDescription":"Is the naming",
            "Project":"D1",
            "ProjectGroup":"D",
            "Task":"T1",
            "IsCheck":true
        },
        {
            "DqcCode":"DQC-109-1-1",
            "DqcDescription":"Checked-In",
            "Project":"D2",
            "ProjectGroup":"D",
            "Task":"T2",
            "IsCheck":true
        }
    ]
    

    enter image description here