0

I am trying to send this data as a JSON Post to an Asp.Net WebApi controller called Dashboard. But with these parameters I am getting the following error. Any idea how to correct this?

{
    "errors": {
        "chartType": [
            "Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[System.Int32]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'chartType', line 2, position 13."
        ]
    },
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "80000864-0007-fd00-b63f-84710c7967bb"
}

JSON:

{ 
    "ChartType": 1,
    "List": [1,2,3]
}

Code:

public void Dashboard(int chartType, List<int> list)
{
    Console.Write("");
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Brian Rice
  • 3,107
  • 1
  • 35
  • 53

1 Answers1

0

You can try to create a class ViewModel

public class ViewModel
{
    public int ChartType { get; set; }
    public List<int> List { get; set; }
}

Then use FromBody attribute.

public void Dashboard([FromBody]ViewModel model)
{
    Console.Write("");
}
D-Shih
  • 44,943
  • 6
  • 31
  • 51