I am trying to make an http post using jquery to the Web API controller, I am posting an array of objects to the controller but when is gets to the server the array is empty.
javascript
var url = "api/actuary/" + $("#ActuaryId").val() + "/documents/";
var inputs = $("#proofOfTraining input[type='checkbox']");
var courseAttended = []
inputs.each(function (ind, val) {
var course = {};
course["IsDone"] = $(val).is(":checked");
course["Title"] = $(val).attr("name");
course["ActuaryId"] = $("#ActuaryId").val();
courseAttended.push(course);
});
console.log(courseAttended)
$.post(url, JSON.stringify({ courseAttended }), function (response) {
console.log(response)
})
post Data
controller
[Route("api/actuary/{actuaryId:long}/documents/")]
[HttpPost]
public async Task<IHttpActionResult> uploadCourseTrainingProofAsync(List<CourseModel> courseAttended)
{
try
{
using (Data.ADPDB db = new Data.ADPDB())
{
foreach (CourseModel course in courseAttended)
{
var tempDoc = new documents();
tempDoc.ActuaryId = course.ActuaryId;
tempDoc.Document = null;
tempDoc.DocumentTypeId = -1;
tempDoc.Done = course.IsDone;
tempDoc.Title = course.Title;
db.documents.Add(tempDoc);
}
await db.SaveChangesAsync();
}
return Ok();
}
catch (Exception ex) {
return InternalServerError(ex.InnerException);
}
}
Model
public class CourseModel
{
public int ActuaryId { get; set; }
public string Title { get; set; }
public bool IsDone { get; set; }
}