简体   繁体   English

我可以将字典作为动作参数传递,但不能在复杂的 object 中传递

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

I want to pass IDictionary<string, List<string>> inside a DTO complex object into a ASP.NET 5 Api action using Postman. 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.首先,我尝试通过正在工作的独奏IDictionary<string, List<string>> 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:有用: 在此处输入图像描述

The problem is I need to wrap wthis dictionary inside complex object like this:问题是我需要像这样在复杂的 object 中包装这个字典:

    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.但是 model.tags 是 null。

I use this request body:我使用这个请求正文:

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

But the model.Tags is null:但是 model.Tags 是 null:

model.Tags 为空

I would try to mark my Tags property as public at first place:我会尝试首先将我的Tags属性标记为公开:

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.如果它仍然不起作用,我认为您需要在这种情况下使用JsonExtensionData属性。 Take a look at this also for more details.看看这个也可以了解更多细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM