One of two parameters returns null

I am using MVC. When I run the debugger and test Postman and hover over a parameter, it shows that the parameter is being passed an empty value for MovieIds, but it CustomerIdworks as expected.

Api Controller: newRentalshows MovieIdshow null when debugging

public class NewRentalsController : ApiController
{
    private ApplicationDbContext _context;

    public NewRentalsController()
    {
        _context = new ApplicationDbContext();
    }

    [HttpPost]
    public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
    {
        var customer = _context.Customers.Single(c => c.Id == newRental.CustomerId);

        var movies = _context.Movies.Where(m => newRental.MovieIds.Contains(m.Id)).ToList();

        //var movies = _context.Movies.Where(m => m.Id ==1);

        foreach (var movie in movies)
        {
            if (movie.NumberAvailable == 0)
                return BadRequest("Movie is not available.");

            movie.NumberAvailable--;

            var rental = new Rental
            {
                Customer = customer,
                Movie = movie,
                DateRented = DateTime.Now
            };

            _context.Rentals.Add(rental);
        }

        _context.SaveChanges();

        return Ok();
    }

}

DTO:

public class NewRentalDto
{
    public int CustomerId { get; set; }
    public List<int> MovieIds { get; set; }
}

Postman Request:

{"customerId": 2,
"movieId": 1,
"dateRented": "2017-09-28T00:00:00"
}

I think this has something to do with List<int>, because I can hardcode the identifier and it will send it to db.

+4
source share
1 answer

As you correctly guessed, the problem is what MovieIdsis List<int>so your request should be like this (use brackets []):

MovieIds: [1],
+1
source

Source: https://habr.com/ru/post/1686839/