I have the following model in asp.net core 2 web api:
public class MainVM
{
public int ID { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
public int DepartmentID { get; set; }
public IFormFile Logo { get; set; }
public List<ChildVM> ListChilds { get; set; }
}
public class ChildVM
{
public string Name { get; set; }
public int SequenceNo { get; set; }
public int NoOfPrices { get; set; }
public IFormFile Image { get; set; }
}
and the endpoint:
[HttpPost]
[Consumes("multipart/form-data")]
public void Post([FromForm]MainVM data)
{
}
Angular6 service that I am using to post data from angular:
addData(mydata: MainVM, logo: File, myImages: File[]): Observable<MainVM> {
const formData: FormData = new FormData();
formData.append('Logo', logo, logo.name);
if (myImages && myImages.length > 0) {
for (var i = 0; i < myImages.length; i++) {
formData.append('ListChilds[].Image', myImages[i], myImages[i].name);
}
}
formData.append('data', new Blob([JSON.stringify(mydata)], { type: 'application/json' }));
console.log(formData);
return this.http.post<MainVM>('http://localhost:60458/api/mycontroller/', formData, httpOptions).pipe(
map((res) => { console.log('res'); return res; }),
catchError(this.handleError('lpc', null))
);
}
Now the problem I am facing is that it only receives Logo on web api, all other fields remains null.
I think I am missing something. Please guide me.
Thank you.