-1

I have this DTO class:

public class TestClass
{
    public int Id {get; set;}
    public Dictionary<string, InnerClass> Values {get; set;}
}

public class InnerClass
{
    public int Id {get; set;}
    public string Name {get; set;}
}

However, when I pass following JSON as a parameter to a Controller, Values is empty:

{
    "id": 1,
    "values": {
        "fistkey":{
            "id": 3,
            "name":"firstname"
        },
        "secondkey":{
            "id": 4,
            "name":"secondname"
        }
    }
}

Do I need a custom value provider or how could I make it populate Values property?

EDIT: Changed the example to more accurate represent my use case. The original example was overly simplified.

wekso
  • 39
  • 7
  • Are you using JSON.NET or System.Text.Json? – Dai Aug 03 '22 at 09:21
  • Also, your DTOs probably shouldn't be mutable... – Dai Aug 03 '22 at 09:22
  • maybe this can help [question](https://stackoverflow.com/questions/20727787/deserialize-json-string-to-dictionarystring-object) – Marco Beninca Aug 03 '22 at 09:26
  • @MarcoBeninca All of the answers in that linked QA concern `Newtonsoft.Json` (aka JSON.NET), not `System.Text.Json` (STJ) but STJ is the default JSON serializer in ASP.NET Core now. – Dai Aug 03 '22 at 09:34
  • How is your controller method looking? I'm assuming it's a `POST` endpoint? Maybe you just forgot the `FromBody` attribute – MindSwipe Aug 03 '22 at 09:40
  • Having attempted a local reproduction of your issue, I can't find any error, it's working as intended, we're going to need more details for your specific case to be able to help out – MindSwipe Aug 03 '22 at 09:42
  • Maybe you are using an old version of .net with an old STJ version? [see this old issue](https://makolyte.com/system-text-json-cant-serialize-dictionary-unless-it-has-a-string-key/) – Martin Aug 03 '22 at 09:44
  • I'm using a custom value provider, which uses Newtonsoft.Json. Also, I'm working with .NET framework, not core. – wekso Aug 03 '22 at 09:47
  • **Exactly** what version of ASP.NET are you using? I assume you're using Web API, but which version? Please also post your Controller/Action's code, and if applicable, your `Global.asax.cs` file too. – Dai Aug 03 '22 at 09:48
  • I tried the same code and the object was properly Deserialized with the specified model and json. Now, You need to explain how you are passing the json to the controller. – Jamshaid K. Aug 03 '22 at 09:58
  • Target framework is 4.8. – wekso Aug 03 '22 at 11:08
  • The real DTO class is a bit more complex than this example. However, there's essentially a similar dictionary which is not populated, although all other properties are populated correctly. – wekso Aug 03 '22 at 11:14
  • You have to show your controler and the views in order to get help. How we can judge by class only? – Serge Aug 03 '22 at 11:49
  • There's no logic in controller, the function is just a one liner. Controller just receives a TestClass as a parameter and sends it to handler function via Mediator. Views are implemented in react and the controller action is called via an ajax call. What might be relevant is the value provider implementation, but stack overflow did not allow me to post it, because after that the message was "mostly code". – wekso Aug 03 '22 at 12:02

1 Answers1

0

The problem was an issue in my model class. In fact, it was defined as

public class TestClass
{
    public int Id {get; set;}
    public Dictionary<string, InnerClass> Values = new Dictionary<string, InnerClass>();
}

The problem was that Values was missing {get; set;}

wekso
  • 39
  • 7