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 CustomerId
works as expected.
Api Controller: newRental
shows MovieIds
how 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();
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.
source
share