Is there any current solution natively built into ASP.NET Core 8 Web Api that allows a json object to be sent as a parameter as well as files and list of files? I am trying to post the following data from axios. My controller looks as follows:
[HttpPost("")]
public async Task<IActionResult> AddDocumentReview([FromForm] UploadedDocumentReviewDTO documentReview)
{
return Ok(certificationReview);
}
My model is as follows:
public class UploadedDocumentReviewDTO
{
[ModelBinder(Name = "review_details")]
public required UploadedReviewDetailsDTO ReviewDetails {get; set;}
[ModelBinder(Name = "document")]
public required IFormFile Document {get; set;}
}
My frontend code trying to use FromForm via FormData is as follows:
const submit = () => {
let form = new FormData();
const reviewDetails : ReviewDetails = {
rating_period_begin_date: ratingPeriodBeginModel.value!,
rating_period_end_date: ratingPeriodEndModel.value!,
document_title: documentTitleModel.value!
}
form.append('review_details' , JSON.stringify(reviewDetails))
form.append('document' , certificationModel.value)
api.post('/DocumentReview', form, config).then(response => {
})
}
As you can see my primary model is complex as it has UploadedReviewDetailsDTO object inside of it. This nested objet does not deserialize to the model. I tested to see if i changed that UploadedReviewDetailsDTO object to a string instead then it binds great but I was hoping it would deserialize all elements. I have found one post on here regarding a custom model binder which was posted in 2017 but I was hoping this has since been resolved with a more native solution built into .net core.