10

I have this code

    [HttpPost("[action]")]
    public IActionResult Add([FromBody] Player player)
    {
        PlayerService.Add(player);
        PlayerService.SaveChanges();
        return Created("Player created",player.Name);
    }

and this json

[
  {
    "name": "Olivier Giroud",
    "league": "Premier League",
    "currentTeam": "Arsenal"
  }
]

and everything is going ok. But if i want to send a json array

[ 
  { "name": "Olivier Giroud", "league": "Premier League", "currentTeam": "Arsenal" },    
  {"name": "Lucas Perez","league": "Premier League","currentTeam":"Arsenal"}
]

i get an exception "Object reference not set to an instance of an object.' ". I tried with List

public IActionResult Add([FromBody] List< Player >  players)    

or

public IActionResult Add([FromBody] IEnumerable < Player >  players)

or

public IActionResult Add([FromBody] Players[] players)

but without success. What should i do?

George Alexandria
  • 2,841
  • 2
  • 16
  • 24
Marian
  • 295
  • 4
  • 13

1 Answers1

6

Try changing

public IActionResult Add([FromBody] Players[] players)

To

public IActionResult Add([FromBody] Player[] players)
jeanfrg
  • 2,366
  • 2
  • 29
  • 40
alinous
  • 61
  • 4