I am able to write a controller for posting 1 object to the database in my controller. However I want to now post multiple objects in 1 API call using JSON
My Entity
public class CustomerInvoiceLine: BaseEntity
{
public SchoolFee SchoolFee { get; set; }
public int SchoolFeeId { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public int Amount { get; set; }
public int CustomerInvoiceId { get; set; }
}
I have a DTO as follows:
public class CustomerInvoiceLineAddDto
{
public int SchoolFeeId { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public int Amount { get; set; }
public int CustomerInvoiceId { get; set; }
}
I have created a Repository:
public async Task AddCustomerInvoiceLineAsync(CustomerInvoiceLine customerInvoiceLine)
{
_context.CustomerInvoiceLines.Add(customerInvoiceLine);
await _context.SaveChangesAsync();
}
And finally the Controller
[HttpPost]
public async Task<ActionResult> AddCustomerInvoiceLine(CustomerInvoiceLineAddDto invoiceLineAddDto)
{
CustomerInvoiceLine invoiceLineDetails = new CustomerInvoiceLine();
_mapper.Map(invoiceLineAddDto, invoiceLineDetails);
await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLineAsync(invoiceLineDetails);
return Ok();
}
The above controller works fine for posting just 1 item in the JSON request.
I then tried to change this to receive a JSON Array.
public async Task<ActionResult> AddCustomerInvoiceLine(CustomerInvoiceLineAddDto invoiceLineAddDto)
{
string json = "";
CustomerInvoiceLine invoiceLineDetails = new CustomerInvoiceLine();
CustomerInvoiceLineAddDto invoiceLines = JsonConvert.DeserializeObject<CustomerInvoiceLineAddDto>( json );
_mapper.Map(invoiceLineAddDto, invoiceLines);
await _unitOfWork.CustomerInvoiceLineRepository.AddCustomerInvoiceLineAsync(invoiceLineDetails);
return Ok();
}
My JSON Request:
[
{
"schoolFeeId": 1,
"customerInvoiceId": 18,
"description": "School Fees for 2022",
"quantity": 1,
"amount": 5000
},
{
"schoolFeeId": 1,
"customerInvoiceId": 18,
"description": "School Fees for 2021",
"quantity": 1,
"amount": 3000
}
]
Could somebody please assist with understanding how to deserialise the JSON body and then process to the database?
public async Task<ActionResult> AddCustomerInvoiceLine(IList<CustomerInvoiceLineAddDto> invoiceLineAddDto)