[SOLVED] I can pass dictionary as action parameter but can't pass it inside complex object

Issue

I want to pass IDictionary<string, List<string>> inside a DTO complex object into a ASP.NET 5 Api action using Postman.

First i tried to pass solo IDictionary<string, List<string>> which is working. Here is how i do it:

I use this request body:

{
    "Key1": [
        "Val1",
        "Val1"
    ],
    "Key2": [
        "Val1",
        "Val1"
    ]
}

And it works well, when I pass it like this to action:

        [HttpPut("{id}")]
        public IActionResult EditTags(int id, [FromBody] IDictionary<string, List<string>> tags)
        {
            return Ok();
        }

It works:
enter image description here

The problem is I need to wrap wthis dictionary inside complex object like this:

    public class DTO
    {
        public string SomeValue{ get; set; }

        IDictionary<string, List<string>> Tags{ get; set; }
     }

And take this in as a parameter to action:

    [HttpPut("{id}")]
    public IActionResult EditTags(int id, [FromBody] DTO model)
    {
        //Do something with the model here
        return Ok();
    }

But the model.tags is null.

I use this request body:

{
    "SomeValue": "Value",
    "Tags": {
        "Key1": [
            "Val1",
            "Val1"
        ],
        "Key2": [
            "Val1",
            "Val1"
        ]
    }
}

But the model.Tags is null:

model.Tags is null

Solution

I would try to mark my Tags property as public at first place:

public IDictionary<string, List<string>> Tags{ get; set; }

if it still doesn’t work, I think you need to use the JsonExtensionData attribute in this case. Take a look at this also for more details.

Answered By – Salah Akbari

Answer Checked By – David Marino (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *